Execute system external command in PHP

Source: Internet
Author: User
Tags php script safe mode



PHP as a server-side scripting language, like writing simple, or complex dynamic Web pages such tasks, it is fully competent. But things are not always the case, sometimes in order to implement a function, you have to rely on the operating system of external programs (or called commands), so you can do more with less.
So is it possible to invoke an external command in a PHP script? If you can, how to do it? What are some of the concerns? I am sure you will be able to answer these questions after reading this article.

Is it OK?

The answer is yes. PHP, like any other programming language, can simply invoke an external command within a program and is simple: just use one or several functions.

Prerequisite conditions

Because PHP is basically for web application development, security is an important aspect of people's thinking. So the PHP designers added a door to PHP: Safe mode. If you are running in Safe mode, the PHP script will be subject to the following four limitations:

Execute external command
There are some restrictions when opening files
Connecting to the MySQL database
HTTP-based Authentication
In safe mode, only external programs in a particular directory can be executed, and calls to other programs are rejected. This directory can be specified in the php.ini file using the Safe_mode_exec_dir directive, or in compiling PHP with the--with-exec-dir option, and the default is/usr/local/php/bin.

If you call an external command that should be able to output the result (meaning that the PHP script is not wrong) and get a blank, it is likely that your network administrator has already run PHP in safe mode.

How to do it?

Calling an external command in PHP can be implemented in three ways as follows:

1 specialized functions provided in PHP

PHP provides a total of 3 functions for executing external commands: System (), exec (), PassThru ().

System ()

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

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

Example:

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 executes the given command without outputting the result, but instead returns the last line of the result. Although it returns only the last line of the command result, the complete result can be obtained with the second parameter array, by appending the result line by row to the end of the array. So if the array is not empty, it's best to use unset () to clear it before calling. You can use the third parameter to obtain the status code of the command execution only if you specify the second parameter.
Example:

EXEC ("/bin/ls-l");
EXEC ("/bin/ls-l", $res);
EXEC ("/bin/ls-l", $res, $RC);
?>

PassThru ()

Prototype: void PassThru (String command [, int return_var])

PassThru () invokes only the command and does not return any results, but outputs the command's running results directly to the standard output device. So the PassThru () function is often used to invoke programs such as the Pbmplus (a tool that processes images under UNIX, outputting the stream of binary raw images). Likewise it can get the status code of the command execution.

Example:

Header ("Content-type:image/gif");
PassThru ("./ppmtogif hunte.ppm");
?>

2 using the Popen () function to open the process

The above method simply executes the command, but cannot interact with the command. But there are times when you have to enter something into the command, such as adding a Linux system user to call Su to change the current user to root, and the SU command must enter the root password on the command line. In this case, it is obviously not possible to use the method mentioned above.

The Popen () function opens a process pipeline to execute the given command, returning a file handle. Now that you are returning a file handle, you can read and write to it. In PHP3, this handle can only be done in a single mode of operation, either written or read; Starting with PHP4, you can read and write at the same time. Unless the handle is open in a pattern (read or write), you must call the Pclose () function to close it.

Example 1:

$FP =popen ("/bin/ls-l",
<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.