Differences between exec and shell_exec functions in PHP

Source: Internet
Author: User
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.

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.