Home >Backend Development >PHP Tutorial >Summary of methods for calling external shell in php 1
When the php program is running in safe mode, the phpscriptis subject to the following four restrictions:
1) Executing external commands
2) There are some restrictions when opening files
3), connect to MySQL database
4) HTTP-based authentication
In safe mode, only external programs in specific directories can be executed, and calls to other programs
will be rejected. This directory can be specified in the php.ini file using the safe_mode_exec_dir directive, or by adding the --with-exec-dir option when compiling PHP. The default is /usr/local/php/bin.
If you call an external command that should be able to output results (meaning that the PHP script is correct), but the result
is blank, then it is very
possible that PHP is running insafe mode
of.How to deal with this situation?
Calling external commands in PHP can be implemented in the following three methods:PHP provides 3 specialized functions for executing external commands:
system( ), exec( ), passthru( ).
system( )
Prototype: string system (string command [, int return_var])system() function is similar to that in other languages. It executes the given command, outputs and returns the result.
The second parameter is optional,
is used to get the status code after the command is executed.
Example:
Copy the code
system("/usr/local/bin/webalizer/webalizer");?> ;
exec()Prototype: string exec (string command [, string array [, int return_var] ] )
exec() function is similar to system() , also executes the given command, but does not output the result, but returns the last line of the result.
Although it only returns the last line of the command result, the complete result can be obtained by using the second parameter array. The
method is to append the result line by line to the end of the array. So if array is not empty
, it is best to useunset() to clear it before calling.
Only when the second parameter is specified, the third parameter can be used to obtain the status code of command execution.
Example:Copy the code
exec("/bin/ls -l");exec("/bin/ ls -l", $res);#$res is a data, each element represents a row of the resultexec("/bin/ls -l", $res, $rc);#The value of $rc is Status code of command /bin/ls -l. In case of success, it is usually 0
?>
passthru()Prototype: void passthru (string command [, int return_var ])
passthru() only calls the command and does not return any results, but directly outputs the running results of the command to the standard
output device
So the passthru() function is often used to call programs like pbmplus (a tool for processing images under Unix
,outputs a binary stream of original images) . It can also get the status code of command execution. Example:
Copy code
The code is as follows:
header("Content-type: image/gif");
?>Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. The above has introduced the summary 1 of the method of calling external shell by PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.