Home > Backend Development > PHP Tutorial > Summary of common functions for executing linux commands in PHP, summary of phplinux commands_PHP tutorial

Summary of common functions for executing linux commands in PHP, summary of phplinux commands_PHP tutorial

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

Summary of commonly used functions for PHP to execute linux commands, summary of phplinux commands

Under normal circumstances, php is rarely used to execute linux commands, but under special circumstances, you may use it these functions. I used to know that there are two functions that can execute linux commands, one is exec and the other is shell_exec. In fact, there are many. Based on the content of the manual, the following 6 functions are introduced.

1, exec function

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

The returned results are as follows:

[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 function

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

Return results:

[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 function

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

4, popen function

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

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

Copy after login

5, proc_open function

<&#63;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);
&#63;>
Copy after login

6, shell_exec function

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

The return results of popen, passthru, proc_open, shell_exec are as follows:

[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

These are the only functions I can find that can execute commands under Linux. I think there should be more. Welcome to add.

Articles you may be interested in:

  • Instructions for using common functions of PHP to execute Linux system commands
  • How to support PHP’s iconv() function on Linux system
  • Use PHP script to encrypt strings with md5 function under Linux

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1098696.htmlTechArticleSummary of common functions for PHP to execute linux commands, summary of phplinux commands In general, php is rarely used to execute linux commands , but in special cases, you may use these functions. Before I...
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