歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

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;

  ?>

Copyright © Linux教程網 All Rights Reserved