PHP調用Linux系統的常用函數
1、exec函數
<?php
$test = "ls /tmp/test"; //ls是linux下的查目錄,文件的命令
exec($test,$array); //執行命令
print_r($array);
?>
2、system函數
<?php
$test = "ls /tmp/test";
$last = system($test);
print "last: $last\n";
?>
3、passthru函數
<?php
$test = "ls /tmp/test";
passthru($test);
?>
4、popen函數
<?php
$test = "ls /tmp/test";
$fp = popen($test,"r"); //popen打一個進程通道
while (!feof($fp)) { //從通道里面取得東西
$out = fgets($fp, 4096);
echo $out; //打印出來
}
pclose($fp);
?>
5、proc_open函數
<?php
$test = "ls /tmp/test";
$arrayarray = array(
array("pipe","r"), //標準輸入
array("pipe","w"), //標準輸出內容
array("pipe","w") //標準輸出錯誤
);
$fp = proc_open($test,$array,$pipes); //打開一個進程通道
echo stream_get_contents($pipes[1]); //爲什麼是$pipes[1],因爲1是輸出內容
proc_close($fp);
?>
6、proc_open函數
<?php
$test = "ls /tmp/test";
$arrayarray = array(
array("pipe","r"), //標準輸入
array("pipe","w"), //標準輸出內容
array("pipe","w") //標準輸出錯誤
);
$fp = proc_open($test,$array,$pipes); //打開一個進程通道
echo stream_get_contents($pipes[1]); //爲什麼是$pipes[1],因爲1是輸出內容
proc_close($fp);
?>
7、shell_exec函數
<?php
$test = "ls /tmp/test";
$out = shell_exec($test);
echo $out;
?>
很多情況下需要php調用其他程序如shell命令、shell腳本、可執行程序等等,此時需要使用到諸如exec/system/popen/proc_open等函數,每種函數有各自適合使用的場景以及需要注意的地方。
前提:PHP沒有運行在安全模式
如果PHP運行在安全模式下,那麼在執行外部命令、打開文件、連接數據庫、基於HTTP的認證這4個方面將會受到制約,可能在調用外部程序時無法獲取預期的結果,此時需要設置特定目錄,可以在php.ini中編輯safe_mode_exec_dir參數來指定。
1. exec
原型:string exec ( string command [, array &output [, int &return_var]] )
描述:返回值保存最後的輸出結果,而所有輸出結果將會保存到$output數組,$return_var用來保存命令執行的狀態碼(用來檢測成功或失敗)。
例子:$ret = exec("ls -al", $output, $var);
注意:
A. 輸出結果會逐行追加到$output中,因此在調用exec之前需要unset($output),特別是循環調用的時候。
B. 如果想通過exec調用外部程序後馬上繼續執行後續代碼,僅僅在命令里加"&"是不夠的,此時exec依然會等待命令執行完畢;需要再將標準輸出做重定向纔可以,例如:exec("ls -al >/dev/null &", $output, $var);
C. 要學會善用EscapeShellCmd()和EscapeShellArg()。函數EscapeShellCmd把一個字符串 中所有可能瞞過Shell而去執行另外一個命令的字符轉義。這些字符在Shell中是有特殊含義的,象分號(|),重定向(>)和從文件讀入 (<)等。函數EscapeShellArg是用來處理命令的參數的。它在給定的字符串兩邊加上單引號,並把字符串中的單引號轉義,這樣這個字符串
就可以安全地作爲命令的參數。
2. system
原型:string system ( string command [, int &return_var] )
描述:執行給定的命令,返回最後的輸出結果;第二個參數是可選的,用來得到命令執行後的狀態碼。
例子:$ret = system("ls -al", $var);
注意:略。
3. passthru
原型:void passthru (string command [, int return_var])
描述:執行給定的命令,但不返回任何輸出結果,而是直接輸出到顯示設備上;第二個參數可選,用來得到命令執行後的狀態碼。
例子:passthru("ls -al", $var);
注意:略。
4. popen
原型:resource popen ( string command, string mode )
描述:打開一個指向進程的管道,該進程由派生給定的 command 命令執行而產生。 返回一個和 fopen() 所返回的相同的文件指針,只不過它是單向的(只能用於讀或寫)並且必須用 pclose() 來關閉。此指針可以用於 fgets(),fgetss() 和 fwrite()。
例子:$fd = popen("command", 'r'); $ret = fgets($fd);
注意:只能打開單向管道,不是'r'就是'w';並且需要使用pclose()來關閉。
5. proc_open
原型:resource proc_open ( string cmd, array descriptorspec, array &pipes [, string cwd [, array env [, array other_options]]] )
描述:與popen類似,但是可以提供雙向管道。具體的參數讀者可以自己翻閱資料,比如該博客:http://hi.baidu.com/alex_wang58/blog/item/a28657de16fec55195ee372a.html。
注意:
A. 後面需要使用proc_close()關閉資源,並且如果是pipe類型,需要用pclose()關閉句柄。
B. proc_open打開的程序作爲php的子進程,php退出後該子進程也會退出。
C. 筆者在使用的時候遇到獲取外部程序輸出阻塞的問題,也就是在例子中的fgets($pipes[1])語句阻塞了,無法繼續進行。經過多方查證後發現,問題一般出在外部程序中,比如外部程序是C程序,使用fprintf(stdin,
"**** \n");輸出結果,此時需要加上fflush(stdout);才行,否則輸出結果可能會暫留緩存中,無法真正輸出,而php也就無法獲取輸出了。
例子:
$pwd = "*****";
$pipes = array();
$command = "*****";
$desc = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
$handle = proc_open($command, $desc, $pipes, $pwd);
if (!is_resource($handle)) {
fprintf(STDERR, "proc_open failed.\n");
exit(1);
}
///< 讀寫
fwrite($pipes[0], "*****\n");
$ret = rtrim(fgets($pipes[1]), "\n");
///< 關閉管道
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($handle);