Home  >  Article  >  Backend Development  >  Summary of commonly used functions for executing linux commands in PHP

Summary of commonly used functions for executing linux commands in PHP

高洛峰
高洛峰Original
2016-12-21 10:34:421543browse

Normally, php is rarely used to execute Linux commands, but under special circumstances, you may use 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. Through this article, I will introduce to you a summary of common functions for executing Linux commands in PHP. Friends who need it can refer to it

In general, PHP is rarely used. Execute linux commands, but in special cases, you may use 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

3, passthru function

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

4, popen function

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

5, proc_open function

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

6, shell_exec function

[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

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

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

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

For more related articles on the summary of commonly used functions for executing Linux commands in PHP, please pay attention to the PHP Chinese website!

Statement:
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