Home >Backend Development >PHP Tutorial >Let's take a look at the process of executing ordinary shell commands in PHP
【Related learning recommendations: php graphic tutorial】
Here are some common shell commands demonstrated
PHP execution shell command, you can use the following functions:
string system ( string $command [, int &$return_var ] )
string exec ( string $command [, array &$output [, int &$return_var ]] )
void passthru ( string $command [, int &$return_var ] )
Note: these three functions are prohibited by default Yes, if you want to use these functions, you must first modify the php configuration file php.ini, search for the keyword disable_functions, delete the function names in this item, and then pay attention to restart apache.
First, let’s take a look at system() and passthru(), which have similar functions and can be interchanged:
<?php $shell = "ls -la"; echo "<pre class="brush:php;toolbar:false">"; system($shell, $status); echo ""; //注意shell命令的执行结果和执行返回的状态值的对应关系 $shell = "$shell"; if( $status ){ echo "shell命令{$shell}执行失败"; } else { echo "shell命令{$shell}成功执行"; } ?>
The execution results are as follows:
Note that system() will display the results immediately after executing the shell command. This is more inconvenient because sometimes we don’t need the results to be output immediately, or even output, so we can use exec()
Examples of using exec():
<?php $shell = "ls -la"; exec($shell, $result, $status); $shell = "<font color='red'>$shell</font>"; echo "<pre class="brush:php;toolbar:false">"; if( $status ){ echo "shell命令{$shell}执行失败"; } else { echo "shell命令{$shell}成功执行, 结果如下<hr>"; print_r( $result ); } echo ""; ?>
The running results are as follows:
Related learning recommendations: php programming (video)
The above is the detailed content of Let's take a look at the process of executing ordinary shell commands in PHP. For more information, please follow other related articles on the PHP Chinese website!