What is the difference between the exec function and the shell_exec function in PHP? execshell_exec

Source: Internet
Author: User

What is the difference between the exec function and the shell_exec function in PHP? execshell_exec

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:
Copy codeThe 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
Copy codeThe Code is as follows:
<? Php
/**
* What is the difference between exec and shell_exec?
* Qiongtai blog
*/
$ Data = exec ('/bin/ls-l ');
Echo '<pre> ';
Print_r ($ data );
Echo '</pre> ';
?>

Execution result
Copy codeThe Code is as follows:
-Rw-r --. 1 lee web 81 Mar 8 c. php

Example of shell_exec
Copy codeThe Code is as follows:
<? Php
/**
* What is the difference between exec and shell_exec?
* Qiongtai blog
*/
$ Data = shell_exec ('/bin/ls-l ');
Echo '<pre> ';
Print_r ($ data );
Echo '</pre> ';
?>

Execution result
Copy codeThe 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.


Execute the shell_exec function in php in linux.

Shell_exec ("/usr/local/bin/%2swf/home/xiazai/03.pdf-o/home/xiazai/1.swf;/usr/local/bin/%2swf/home/xiazai/2.jpg- o/home/xiazai/2.swf ");
Add a semicolon to each command

How to enable the EXEC function in PHPini

First, disable the security mode safe_mode = off.

Then look at the disabled function list.
Disable_functions = proc_open, popen, exec, system, shell_exec, passthru

Here we need to remove exec
Restart apache.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.