This article mainly introduces the differences between the exec and shell_exec functions in PHP. These two functions are very dangerous and are usually disabled. Of course they will also be used in special cases, for more information, see
This article mainly introduces the differences between the exec and shell_exec functions in PHP. These two functions are very dangerous and are usually disabled. Of course they will also be used in special cases, for more information, see
Both of these functions execute Linux Command functions. The difference is that the returned results are different. exec can only obtain the last row of data, while shell_execu can obtain all the data.
Assume that the script path contains the following files:
The Code is as follows:
-Bash-4.1 # ll
Total usage 12
-Rw-r --. 1 www web 133 July 16 15:00 a. php
-Rw-r --. 1 lee web 59 February 29 17:05 B. php
-Rw-r --. 1 lee web 81 March 8 17:00 c. php
Exec example
The Code is as follows:
<? Php
/**
* What is the difference between exec and shell_exec?
* Qiongtai blog
*/
$ Data = exec ('/bin/ls-l ');
Echo'
';
print_r($data);
echo '
';
?>
Execution result
The Code is as follows:
-Rw-r --. 1 lee web 81 Mar 8 c. php
Example of shell_exec
The Code is as follows:
<? Php
/**
* What is the difference between exec and shell_exec?
* Qiongtai blog
*/
$ Data = shell_exec ('/bin/ls-l ');
Echo'
';
print_r($data);
echo '
';
?>
Execution result
The Code is as follows:
Total 12
-Rw-r --. 1 www web 139 Jul 16 2012 a. php
-Rw-r --. 1 lee web 59 Feb 29 :05 B. php
-Rw-r --. 1 lee web 81 Mar 8 c. php
Therefore, when using the exec function, note that if you need to obtain all the returned information, you should use the shell_exec function. Of course, if the command execution result contains only one row of returned information, it doesn't matter which one to use.