php執行linux命令的6個函數

來源:互聯網
上載者:User

標籤:介紹   comm   情況   web   bsp   手冊   pes   gets   linux   

一般情況下,很少會用php去執行linux命令,不過特殊情況下,你也許會用到這些函數。以前我知道有二個函數可以執行linux命令,一個是exec,一個是shell_exec。其實有很多的,結合手冊內容,介紹以下6個函數。

 

1,exec函數

  1. <?php  
  2. $test = "ls /tmp/test";   //ls是linux下的查目錄,檔案的命令  
  3. exec($test,$array);       //執行命令  
  4. print_r($array);  
  5. ?>  

返回結果如下:

  1. [[email protected] shell]# php ./exec.php  
  2. Array  
  3. (  
  4.  [0] => 1001.log  
  5.  [1] => 10.log  
  6.  [2] => 10.tar.gz  
  7.  [3] => aaa.tar.gz  
  8.  [4] => mytest  
  9.  [5] => test1101  
  10.  [6] => test1102  
  11.  [7] => weblog_2010_09  
  12. )  

2,system函數

  1. <?php  
  2. $test = "ls /tmp/test";  
  3. $last = system($test);  
  4. print "last: $last\n";  
  5. ?>  

返回結果:

  1. [[email protected] shell]# php system.php  
  2. 1001.log  
  3. 10.log  
  4. 10.tar.gz  
  5. aaa.tar.gz  
  6. mytest  
  7. test1101  
  8. test1102  
  9. weblog_2010_09  
  10. last:weblog_2010_09  

3,passthru函數

  1. <?php  
  2. $test = "ls /tmp/test";  
  3. passthru($test);  
  4. ?>  

4,popen函數

  1. <?php  
  2. $test = "ls /tmp/test";  
  3. $fp = popen($test,"r");  //popen打一個進程通道  
  4.   
  5. while (!feof($fp)) {      //從通道裡面取得東西  
  6.  $out = fgets($fp, 4096);  
  7.  echo  $out;         //列印出來  
  8. }  
  9. pclose($fp);  
  10. ?>  

5,proc_open函數

  1. <?php  
  2. $test = "ls /tmp/test";  
  3. $array =   array(  
  4.  array("pipe","r"),   //標準輸入  
  5.  array("pipe","w"),   //標準輸出內容  
  6.  array("pipe","w")    //標準輸出錯誤  
  7.  );  
  8.   
  9. $fp = proc_open($test,$array,$pipes);   //開啟一個進程通道  
  10. echo stream_get_contents($pipes[1]);    //為什麼是$pipes[1],因為1是輸出內容  
  11. proc_close($fp);  
  12. ?>  

6,shell_exec函數

  1. <?php  
  2. $test = "ls /tmp/test";  
  3. $out = shell_exec($test);  
  4. echo $out;  
  5. ?>  

popen,passthru,proc_open,shell_exec的返回結果如下:

  1. [[email protected] shell]# php test.php  
  2. 1001.log  
  3. 10.log  
  4. 10.tar.gz  
  5. aaa.tar.gz  
  6. mytest  
  7. test1101  
  8. test1102  
  9. weblog_2010_09  

php執行linux命令的6個函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.