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

Summary of commonly used functions for executing linux commands in PHP

高洛峰
Release: 2023-03-03 19:22:01
Original
1605 people have browsed it

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

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
)
Copy after login

5, proc_open function

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

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
Copy after login

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

<?php
$test = "ls /tmp/test";
passthru($test);
?>
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.

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!

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