Home > Article > Backend Development > Use exec, system and other functions to call system commands in php_PHP tutorial
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.
Let’s first look at the explanation of these two functions in the PHP manual:
Exec --- Execute external program
Syntax: string exec ( string command [, array &output [, int &return_var]] )
Description:
exec() executes the given command command, but it does not output anything. It simply returns the last line from the result of the command. If you need to execute a command and get all the information from the command , you can use the passthru() function.
If the parameter 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 end of the array. 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 to 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 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 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 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 outputting each line.
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.
These two are used to call system shell commands,
Differences:
Exec can return all execution results to the $output function (array). $status is the execution status. 0 is success and 1 is failure
Systerm does not need to provide the $output function. It returns the result directly. Similarly, $return_var is the execution status code. 0 is success and 1 is failure
exec example:
<ol class="dp-c"> <li class="alt"><span><span>以下为引用的内容: </span></span></li> <li><span><?php </span></li><li class="alt"><span class="vars">$a</span><span> = </span><span class="func">exec</span><span>(</span><span class="string">"dir"</span><span>,</span><span class="vars">$out</span><span>,</span><span class="vars">$status</span><span>); </span></li><li><span>print_r(</span><span class="vars">$a</span><span>); </span></li><li class="alt"><span>print_r(</span><span class="vars">$out</span><span>); </span></li><li><span>print_r(</span><span class="vars">$status</span><span>); </span></li><li class="alt"><span>?> </span></li> </ol>
System example:
<ol class="dp-c"> <li class="alt"><span><span>以下为引用的内容: </span></span></li> <li><span><?php </span></li><li class="alt"><span class="vars">$a</span><span> = system(</span><span class="string">"dir"</span><span>,</span><span class="vars">$out</span><span>); </span></li><li><span>print_r(</span><span class="vars">$a</span><span>); </span></li><li class="alt"><span>print_r(</span><span class="vars">$out</span><span>); </span></li><li><span>?> </span></li> </ol>
You can run it to see the effect