PHP执行linux命令常用函数汇总,phplinux命令汇总_PHP教程

WBOY
Release: 2016-07-12 08:59:28
Original
1124 people have browsed it

PHP执行linux命令常用函数汇总,phplinux命令汇总

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

1,exec函数

<?php 
$test = "ls /tmp/test"; //ls是linux下的查目录,文件的命令
exec($test,$array); //执行命令
print_r($array);
?>
Copy after login

返回结果如下:

[root@krlcgcms01 shell]# php ./exec.php 
Array 
( 
[0] => 1001.log 
[1] => 10.log 
[2] => 10.tar.gz 
[3] => aaa.tar.gz 
[4] => mytest 
[5] => test1101 
[6] => test1102 
[7] => weblog_2010_09 
)
Copy after login

2,system函数

<?php 
$test = "ls /tmp/test";
$last = system($test);
print "last: $last\n";
?>
Copy after login

返回结果:

[root@krlcgcms01 shell]# php system.php 
1001.log 
10.log 
10.tar.gz 
aaa.tar.gz 
mytest 
test1101 
test1102 
weblog_2010_09 
last:weblog_2010_09
Copy after login

3,passthru函数

<?php 
$test = "ls /tmp/test";
passthru($test);
?>
Copy after login

4,popen函数

<?php 
$test = "ls /tmp/test";
$fp = popen($test,"r"); //popen打一个进程通道

while (!feof($fp)) { //从通道里面取得东西
$out = fgets($fp, 4096);
echo $out; //打印出来
} 
pclose($fp);
?>

Copy after login

5,proc_open函数

<?php 
$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);
?>
Copy after login

6,shell_exec函数

<?php 
$test = "ls /tmp/test";
$out = shell_exec($test);
echo $out;
?>
Copy after login

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

[root@krlcgcms01 shell]# php test.php 
1001.log 
10.log 
10.tar.gz 
aaa.tar.gz 
mytest 
test1101 
test1102 
weblog_2010_09
Copy after login

我能发现的就这几个函数,能执行linux下的命令,我想应当还有吧,欢迎大家补充。

您可能感兴趣的文章:

  • PHP执行linux系统命令的常用函数使用说明
  • linux系统上支持php的 iconv()函数的方法
  • 利用PHP脚本在Linux下用md5函数加密字符串的方法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1098696.htmlTechArticlePHP执行linux命令常用函数汇总,phplinux命令汇总 一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数。以前我...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!