Home >Backend Development >PHP Tutorial >The difference between the exec function and the shell_exec function in PHP, execshell_exec_PHP tutorial

The difference between the exec function and the shell_exec function in PHP, execshell_exec_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:20:351063browse

The difference between the exec function and the shell_exec function in PHP, execshell_exec

These two functions are both functions for executing Linux commands. The difference is that the return results are different. exec can only get the last line of data, while shell_execu can get all the data.

If there are the following files in the script path:

Copy code The code is as follows:

-bash-4.1# ll
Total usage 12
-rw-rw-r--. 1 www web 133 July 16 15:00 a.php
-rw-r--r--. 1 lee web 59 February 29 17:05 b.php
-rw-r--r--. 1 lee web 81 March 8 17:00 c.php

exec example
Copy code The code is as follows:

/**
* The difference between exec and shell_exec
* Qiongtai Blog
​*/
$data = exec('/bin/ls -l');
echo '
';<br>
print_r($data);<br>
echo '
';
?>

Execution results
Copy code The code is as follows:

-rw-r--r--. 1 lee web 81 Mar 8 17:00 c.php

shell_exec example
Copy code The code is as follows:

/**
* The difference between exec and shell_exec
* Qiongtai Blog
​*/
$data = shell_exec('/bin/ls -l');
echo '
';<br>
print_r($data);<br>
echo '
';
?>

Execution results
Copy code The code is as follows:

total 12
-rw-rw-r--. 1 www web 139 Jul 16 2012 a.php
-rw-r--r--. 1 lee web 59 Feb 29 17:05 b.php
-rw-r--r--. 1 lee web 81 Mar 8 17:00 c.php

So those who usually use the exec function should pay attention. If you need to get all the return information, you should use the shell_exec function. Of course, if the command execution result only has one line of return information, it doesn't matter which one you use.

Execute shell_exec function in php in linux

shell_exec("/usr/local/bin/pdf2swf /home/xiazai/03.pdf -o /home/xiazai/1.swf; /usr/local/bin/jpeg2swf /home/xiazai/2.jpg -o /home /xiazai/2.swf ");
Add a semicolon after each command

How to enable the EXEC function in PHPini

The first thing is to turn off the safe mode safe_mode = off

Then look at the list of disabled functions
disable_functions = proc_open, popen, exec, system, shell_exec, passthru

Here it is Remove exec
Restart apache and it will be OK

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/866665.htmlTechArticleThe difference between the exec function and the shell_exec function in PHP, execshell_exec Both functions execute Linux command functions, different The return result is different. exec can only get the last row number...
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