PHP执行linux命令的6个函数

回复 星标
更多
PHP执行linux命令的6个函数
.
.
.
.
.
.

一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数。以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec。其实有很多的,结合手册内容,介绍以下6个函数。

1,exec函数
$test = "ls /tmp/test";   //ls是linux下的查目录,文件的命令
exec($test,$array);       //执行命令
print_r($array);

返回结果如下:

2,system函数
$test = "ls /tmp/test";
$last = system($test);
print "last: $last\n";

返回结果:

3,passthru函数
$test = "ls /tmp/test";
passthru($test);
4,popen函数
$test = "ls /tmp/test";
$fp = popen($test,"r");  //popen打一个进程通道
      
while (!feof($fp)) {      //从通道里面取得东西
 $out = fgets($fp, 4096);
 echo  $out;         //打印出来
}  
pclose($fp);
5,proc_open函数
$test = "ls /tmp/test";
$array =   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,shell_exec函数
$test = "ls /tmp/test";
$out = shell_exec($test);
echo $out;

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


来自:http://blog.51yip.com/php/1064.html

新窗口打开 关闭