PHP's built-in function Exec,system can invoke system commands (shell commands) and, of course, passthru,escapeshellcmd functions.
In many cases, using functions such as PHP's Exec,system to call system commands can help us get things done faster and better. For example, the first two days of the author in batch processing. rar file exec helped me a lot.
Today, sort out the common calling system functions to share experiences.
Note: In order to use the security mode in the two functions php.ini must be closed, otherwise PHP is not allowed to invoke system commands for security purposes.
Let's take a look at the PHP Manual's explanation of these two functions:
EXEC---Execute an external program
Syntax: string exec (String command [, array &output [, int &return_var]])
Description
EXEC () executes the command commands given, but it does not output anything, it simply returns the last line from the result of the command, and if you need to execute a command and get all the data from the command, you can use the PassThru () function.
If a parameter array is given, the specified array is filled with each row that the command outputs. Note: If the array has previously contained elements, EXEC () will append it to the array, if you do not want the function to append elements, you can pass this array to exec () Call Unset () before.
If a parameter array and Return_var are given, the state command to return execution 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 determine that this user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and you want to leave it in the background (background), you must make sure that the output of the program is turned (redirected) to a file or some output stream, otherwise PHP will hang (hang) Until the program execution is finished.
System---Execute external programs and display output
Syntax: string system (String command [, int &return_var])
Description
System () executes the given command and outputs the result. If a parameter return_var is given, the status code executing the 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 determine that this user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and you want to leave it in the background (background), you must make sure that the output of the program is turned (redirected) to a file or some output stream, otherwise PHP will hang (hang) Until the program execution is finished.
If PHP is operating as a server module, System () will try to automatically clear the output buffer of the Web server after each row is output.
Success is returned to the last line of the command, and false is returned.
If you need to execute a command and get all the data from the command, you can use the PassThru () function.
These two are all used to invoke the system shell command,
Different points:
EXEC can return all the results of execution to the $output function (array), $status is executed state 0 for success 1 for failure
Systerm does not need to provide the $output function, he is directly returning the result, same $return_var is executing the status code 0 for success 1 for failure
Exec Example:
Copy Code code as follows:
<?php
$a = exec ("dir", $out, $status);
Print_r ($a);
Print_r ($out);
Print_r ($status);
?>
System Example:
Copy Code code as follows:
<?php
$a = System ("dir", $out);
Print_r ($a);
Print_r ($out);
?>
system, EXCE, PassThru difference
System () output and returns the last line of shell results.
EXEC () does not output the result, returns the last line of shell results, and all results can be saved to a returned array.
PassThru () Simply invokes the command, outputting the command's results directly to the standard output device.
Same point: All can get the status Code of command execution