How to use exec, system and other functions to call system commands in PHP (not recommended and may cause security issues)_PHP Tutorial

WBOY
Release: 2016-07-21 15:16:54
Original
807 people have browsed it

PHP's built-in functions exec and system can call system commands (shell commands), and of course there are functions such as passthru and escapeshellcmd.

In many cases, using PHP's exec, system and other functions to call system commands can help us complete our work better and faster. For example, exec helped me a lot when I was batch processing .rar files two days ago.

Today I will sort out the commonly used calling system functions and share my experience with everyone.

Note: If you want to use these two functions, the safe mode in php.ini must be turned off, otherwise php will not allow you to call system commands for security reasons.

First take a look at the explanation of these two functions in the PHP manual:

 exec --- Execute external programs

Syntax: string exec ( string command [, array &output [, int &return_var]] )

Explanation:

exec() executes the given command command, but it does not output anything, it simply takes the command The last line is returned in the result. If you need to execute a command and get all the data from the command, you can use the passthru() function.

If the argument array is given, the specified array will be filled with each line output by the command. Note: If the array already contains some elements, exec() will append it to the array. later, if you don't want this function to append elements, you can call unset() before passing the array to exec().

If the parameters array and return_var are given, the status command returned will be written to this variable.

Note: If you allow data from user input to be passed to this function, then you should use escapeshellcmd() to ensure that the user cannot trick the system into executing arbitrary commands. .

Note: If you use this function to start a program and want to leave it while running in the background, you must make sure that the output of the program is redirected to a file or some other Output data stream, otherwise PHP will hang until the program execution ends.

 system --- Execute external programs and display output

Syntax: string system ( string command [, int &return_var] )

Description:

System() executes the given command command and outputs the result. If the parameter return_var is given, the status code of the executed command will be written to this variable.

Note: If you allow data from user input to be passed to this function, then you should use escapeshellcmd() to ensure that the user cannot trick the system into executing arbitrary commands. .

Note: If you use this function to start a program and want to leave it while running in the background, you must make sure that the output of the program is redirected to a file or some other Output data stream, otherwise PHP will hang until the program execution ends.

If PHP is running as a server module, system() will try to automatically clear the web server's output buffer after each line is output.

Returns the last line of the command if successful, false if failed.

If you need to execute a command and get all the data from the command, you can use the passthru() function.

Both of these are used to call system shell commands,

The difference:

exec can return all execution results to the $output function (array), $status is the status of execution, 0 is success, 1 is failure

Systerm does not need to provide the $output function, it returns the result directly. Similarly, $return_var is the status code of execution, 0 is success, 1 is failure

exec example:

Copy code The code is as follows:

< ?php
$a = exec("dir",$out,$status);
print_r($a);
print_r($out);
print_r($status);
?>

system example:
Copy code The code is as follows:

$a = system("dir",$out);
print_r($a);
print_r($out);
?>

The difference between system, exce, and passthru
system() outputs and returns the last line of shell results.
exec() does not output results and returns the last line of shell results. All results can be saved in a returned array.
passthru() only calls the command and outputs the result of the command directly to the standard output device as is.
Same point: you can get the status code of command execution

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325819.htmlTechArticlephp’s built-in functions exec and system can call system commands (shell commands), and of course passthru, escapeshellcmd, etc. function. In many cases, php's exec, system and other functions are used to call system commands...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!