2014年7月20日星期日

php执行linux命令的几个函数

1,exec函数
  1. $test = "ls /tmp/test";   //ls是linux下的查目录,文件的命令  
  2. exec($test,$array);       //执行命令  
  3. print_r($array);  
  4. ?>  
返回结果如下:
  1. [root@krlcgcms01 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. $test = "ls /tmp/test";  
  2. $last = system($test);  
  3. print "last: $last\n";  
  4. ?>  
返回结果:
  1. [root@krlcgcms01 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. $test = "ls /tmp/test";  
  2. passthru($test);  
  3. ?>  
4,popen函数
  1. $test = "ls /tmp/test";  
  2. $fp = popen($test,"r");  //popen打一个进程通道  
  3.   
  4. while (!feof($fp)) {      //从通道里面取得东西  
  5.  $out = fgets($fp, 4096);  
  6.  echo  $out;         //打印出来  
  7. }  
  8. pclose($fp);  
  9. ?>  
5,proc_open函数
  1. $test = "ls /tmp/test";  
  2. $array =   array(  
  3.  array("pipe","r"),   //标准输入  
  4.  array("pipe","w"),   //标准输出内容  
  5.  array("pipe","w")    //标准输出错误  
  6.  );  
  7.   
  8. $fp = proc_open($test,$array,$pipes);   //打开一个进程通道  
  9. echo stream_get_contents($pipes[1]);    //为什么是$pipes[1],因为1是输出内容  
  10. proc_close($fp);  
  11. ?>  
6,shell_exec函数
  1. $test = "ls /tmp/test";  
  2. $out = shell_exec($test);  
  3. echo $out;  
  4. ?>  
popen,passthru,proc_open,shell_exec的返回结果如下:
  1. [root@krlcgcms01 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  

没有评论:

发表评论

在PVE使用虚拟 USB 存储设备 + ARPL 引导 DSM

  部分内容来自: https://blog.myds.cloud/archives/synoboot-usb-image-in-pve-virtual-machine.html 虚拟机建好之后,打开配置文件 nano /etc/pve/qemu-server/100.conf ...