php system()和exec()差別


一、exec  ---執行外部程序

string exec ( string $command [, array &$output [, int &$return_var ]] )

$command   要執行的shell 命令

$output        shell命令的輸出填充此數組,每行輸出填充數組中的一個元素。 請注意,如果數組中已經包含了部分元素,exec() 函數會在數組末尾追加內容。如果你不想在數組末尾進行追                       加,請在傳入 exec() 函數之前 對數組使用 unset() 函數進行重置。

$return_var  命令執行后的返回狀態,命令執行成功值是0

返回值         shell命令輸出的最后一行

ps:   2>&1  exec不成功,調試方案一個技巧就是使用管道命令, 使用 2>&1, 命令就會輸出shell執行時的錯誤到$output變量, 輸出該變量即可分析。

 

 例子1

(1)代碼所在的index.php 文件的結構

  

(2)代碼

  

$out = [34];
$res = exec('ls 2>&1',$out,$return_status);
var_dump($res);
echo '------';
var_dump($out);
echo '------';
var_dump($return_status);

(3)執行結果

zhangxueqing:demo playcrab$ php  ./1/index.php
/Users/playcrab/www/demo/1/index.php:10:
string(11) "webuploader"
------/Users/playcrab/www/demo/1/index.php:12:
array(10) {
  [0] =>
  int(34)
  [1] =>
  string(1) "1"
  [2] =>
  string(6) "1.html"
  [3] =>
  string(5) "1.php"
  [4] =>
  string(10) "client.php"
  [5] =>
  string(14) "design-pattern"
  [6] =>
  string(3) "img"
  [7] =>
  string(17) "jquery.blockUI.js"
  [8] =>
  string(10) "static.php"
  [9] =>
  string(11) "webuploader"
}
------/Users/playcrab/www/demo/1/index.php:14:
int(0)

 二、system ---執行外部程序,並且顯示輸出

 

1 string system ( string $command [, int &$return_var ] )

$command  要執行的命令

$return_var  命令執行后的返回狀態,值是0表示成功

 1.示例代碼

$res = system('ls 2>&1',$return_status);
var_dump($res);
echo '------';
var_dump($return_status);

2.輸出結果

 

 

 

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM