PHP執(zhí)行l(wèi)inux命令6個(gè)函數(shù)代碼實(shí)例
一般情況下,很少會(huì)用php去執(zhí)行l(wèi)inux命令,不過(guò)特殊情況下,你也許會(huì)用到這些函數(shù)。以前我知道有二個(gè)函數(shù)可以執(zhí)行l(wèi)inux命令,一個(gè)是exec,一個(gè)是shell_exec。其實(shí)有很多的,結(jié)合手冊(cè)內(nèi)容,介紹以下6個(gè)函數(shù)。
1,exec函數(shù)
<?php $test = 'ls /tmp/test'; //ls是linux下的查目錄,文件的命令 exec($test,$array); //執(zhí)行命令 print_r($array); ?>
返回結(jié)果如下:
[root@krlcgcms01 shell]# php ./exec.php Array ( [0] => 1001.log [1] => 10.log [2] => 10.tar.gz [3] => aaa.tar.gz [4] => mytest [5] => test1101 [6] => test1102 [7] => weblog_2010_09 )
2,system函數(shù)
<?php $test = 'ls /tmp/test'; $last = system($test); print 'last: $lastn'; ?>
返回結(jié)果:
[root@krlcgcms01 shell]# php system.php 1001.log 10.log 10.tar.gz aaa.tar.gz mytest test1101 test1102 weblog_2010_09 last:weblog_2010_09
3,passthru函數(shù)
<?php $test = 'ls /tmp/test'; passthru($test); ?>
4,popen函數(shù)
<?php $test = 'ls /tmp/test'; $fp = popen($test,'r'); //popen打一個(gè)進(jìn)程通道 while (!feof($fp)) { //從通道里面取得東西 $out = fgets($fp, 4096); echo $out; //打印出來(lái) } pclose($fp); ?>
5,proc_open函數(shù)
<?php $test = 'ls /tmp/test'; $array = array( array('pipe','r'), //標(biāo)準(zhǔn)輸入 array('pipe','w'), //標(biāo)準(zhǔn)輸出內(nèi)容 array('pipe','w') //標(biāo)準(zhǔn)輸出錯(cuò)誤 ); $fp = proc_open($test,$array,$pipes); //打開(kāi)一個(gè)進(jìn)程通道 echo stream_get_contents($pipes[1]); //為什么是$pipes[1],因?yàn)?是輸出內(nèi)容 proc_close($fp); ?>
6,shell_exec函數(shù)
<?php $test = 'ls /tmp/test'; $out = shell_exec($test); echo $out; ?>
popen,passthru,proc_open,shell_exec的返回結(jié)果如下:
[root@krlcgcms01 shell]# php test.php 1001.log 10.log 10.tar.gz aaa.tar.gz mytest test1101 test1102 weblog_2010_09
我能發(fā)現(xiàn)的就這幾個(gè)函數(shù),能執(zhí)行l(wèi)inux下的命令,我想應(yīng)當(dāng)還有吧,歡迎大家補(bǔ)充。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門的常見(jiàn)問(wèn)題(三)2. XML 增、刪、改和查示例3. XML 非法字符(轉(zhuǎn)義字符)4. WMLScript的語(yǔ)法基礎(chǔ)5. JavaScript中顏色模型的基礎(chǔ)知識(shí)與應(yīng)用詳解6. 不要在HTML中濫用div7. ASP動(dòng)態(tài)include文件8. el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決9. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼10. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)
