PHP執行普通shell命令流程解析
這里演示一些普通的shell命令
php執行shell命令,可以使用下面幾個函數:
string system ( string $command [, int &$return_var ] )string exec ( string $command [, array &$output [, int &$return_var ]] )void passthru ( string $command [, int &$return_var ] )
注意的是:這三個函數在默認的情況下,都是被禁止了的,如果要使用這幾個函數,就要先修改php的配置文件php.ini,查找關鍵字disable_functions,將這一項中的這幾個函數名刪除掉,然后注意重啟apache。
首先看一下system()和passthru()兩個功能類似,可以互換:
<?php $shell = 'ls -la'; echo '<pre>'; system($shell, $status); echo '</pre>'; //注意shell命令的執行結果和執行返回的狀態值的對應關系 $shell = '<font color=’red’>$shell</font>'; if( $status ){ echo 'shell命令{$shell}執行失敗'; } else { echo 'shell命令{$shell}成功執行'; }?>
執行結果如下:
注意,system()會將shell命令執行之后,立馬顯示結果,這一點會比較不方便,因為我們有時候不需要結果立馬輸出,甚至不需要輸出,于是可以用到exec()
exec()的使用示例:
<?php $shell = 'ls -la'; exec($shell, $result, $status); $shell = '<font color=’red’>$shell</font>'; echo '<pre>'; if( $status ){ echo 'shell命令{$shell}執行失敗'; } else { echo 'shell命令{$shell}成功執行, 結果如下<hr>'; print_r( $result ); } echo '</pre>';?>
運行結果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
