Home  >  Article  >  Backend Development  >  PHP calls the shell method, PHP calls the shell method_PHP tutorial

PHP calls the shell method, PHP calls the shell method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:072374browse

php calls the shell method, php calls the shell method

The example in this article describes the method of calling shell in PHP and is shared with everyone for your reference. The specific method is as follows:

1. Configuration

Check whether the safe mode is turned on in the configuration in php.ini, mainly in the following three places
safe_mode = (If this is off, you don’t need to worry about the following two)
disable_functions =
safe_mode_exec_dir=

2. Use

Since PHP is basically used for WEB program development, security has become an important aspect that people consider. So PHP designers added a door to PHP: safe mode. If running in safe mode, the PHP script will be subject to the following four restrictions:

① Execute external commands
② There are some restrictions when opening files
③ Connect to MySQL database
④ HTTP-based authentication

In safe mode, only external programs in specific directories can be executed, and calls to other programs will be denied. This directory can be specified using the safe_mode_exec_dir directive in the php.ini file, 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 there are no errors in the PHP script), but you get a blank, then it is likely that your network administrator has run PHP in safe mode.

3. How to do it?

To call external commands in PHP, you can use the following three methods:

1) Use the special functions provided by PHP

PHP provides a total of three specialized functions for executing external commands: system(), exec(), and passthru().

system()

Prototype: string system (string command [, int return_var])

The system() function is similar to that in other languages. It executes the given command, outputs and returns the result. The second parameter is optional and is used to get the status code after the command is executed.

Example:

Copy code The code is as follows:
system("/usr/local/bin/webalizer/webalizer");


exec()

Prototype: string exec (string command [, string array [, int return_var]])

The exec() function is similar to system(), and 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, using the second parameter array can get the complete result by appending the results line by line to the end of the array. So if the array is not empty, it is best to use unset() to clear it before calling it. Only when the second parameter is specified, the third parameter can be used to obtain the status code of command execution.

Example:

Copy code The code is as follows:
exec("/bin/ls -l");
exec("/bin/ls -l", $res);
#$res is a data, each element represents a row of the result
exec("/bin/ls -l", $res, $rc);
The value of #$rc is the status code of the 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 outputs the running results of the command directly to the standard output device as is. Therefore, the passthru() function is often used to call programs like pbmplus (a tool for processing images under Unix that 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");
passthru("./ppmtogif hunte.ppm");

I hope this article will be helpful to everyone’s PHP programming design.

How to make php execute shell

php provides us with three functions: system(), exec(), and passthru() to call external commands.
Although these three commands can execute shell commands of the Linux system, in fact they are The difference:
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 command execution result directly to the standard output device as is.
Same point: you can get the status code of command execution

Example: system("/usr/local/bin/webalizer/webalizer");

How to call SHELL command using PHP? After calling and executing the SHELL command?

Unblock functions such as sysyem() passthru() exec().
Execute passthru('ps -ef'); to get the output of this command. Regularly get what you need, and then call these functions to perform the operation. Please refer to the manual for specific usage.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906677.htmlTechArticleHow to call the shell in php, how to call the shell in php This article describes the method of calling the shell in php, share it with everyone For everyone’s reference. The specific method is as follows: 1. Configuration View the configuration in php.ini...
Statement:
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