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. 什么是python的自省2. php模擬實(shí)現(xiàn)斗地主發(fā)牌3. vue 使用localstorage實(shí)現(xiàn)面包屑的操作4. spring acegi security 1.0.0 發(fā)布5. 理解PHP5中static和const關(guān)鍵字6. Python random庫(kù)使用方法及異常處理方案7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. Vuex localStorage的具體使用9. MyBatis中的JdbcType映射使用詳解10. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼

網(wǎng)公網(wǎng)安備