Usage of system, exec, passthru functions in PHP

Source: Internet
Author: User


Php provides the system (), exec (), passthru () functions to call external commands.

Their differences:

System () outputs and returns the last shell result.
Exec () does not output results. The last shell result is returned. All results can be saved to a returned array.
Passthru () only calls the command and directly outputs the command running result to the standard output device.
Similarities: both of them can obtain the command execution status code.


Ystem (), exec (), shell_exec () official files? As shown in the following figure:

System-Execute an external program and display the output
String system (string $ command [, int & $ return_var])
Exec-Execute an external program
String exec (string $ command [, array & $ output [, int & $ return_var])
Shell_exec-Execute command via shell and return the complete output as a string
String shell_exec (string $ cmd)
The official ystem (), exec (), and shell_exec () files are described as follows:

System-Execute an external program and display the output
String system (string $ command [, int & $ return_var])
Exec-Execute an external program
String exec (string $ command [, array & $ output [, int & $ return_var])
Shell_exec-Execute command via shell and return the complete output as a string
String shell_exec (string $ cmd)
Generally, the system has two types of output: return code and output string.

System ()
$ Last_line = system ('Ls', $ return_var );
System () directly prints the Output Content. Therefore, all the returned content will be displayed on the webpage.
$ Last_line: only the content of the last row can be obtained.
$ Return_var: Gets the system status return code
Exec ()
Exec ('Ls', $ output, $ return_var );
$ Output: The Returned content will be stored in this variable (stored as an array) and will not be displayed directly on the page.
$ Return_var: Gets the system status return code
Shell_exec ()
$ Output = shell_exec ('Ls ');
$ Output: The Returned content will be stored in this variable (stored as plain text) and will not be displayed directly on the page.

You can use the following three methods to call external commands in PHP:

1) use special functions provided by PHP
PHP provides 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 the one in other languages. It executes the given command, outputs and returns the result. The second parameter is optional and is used to obtain the status code after the command is executed.

Returned results

0 is returned for success,
Failed (the command does not exist, and other reasons). A non-0 value is returned.

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

Exec ()

Prototype: string exec (string command [, string array [, int return_var])
The exec () function is similar to system (). It 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 using the second parameter array by appending the result row by row to the end of the array. Therefore, if the array is not empty, we recommend that you use unset () to clear it before calling it. Only when the second parameter is specified can the third parameter be used to obtain the command execution status code.

Example:

The code is as follows: Copy code
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 () only calls the command and does not return any results, but directly outputs the running result of the command to the standard output device as is. Therefore, the passthru () function is often used to call programs such as pbmplus (a Unix-based image processing tool that outputs binary original image streams. It can also get the status code of command execution.

Example:
Header ("Content-type: image/gif ");
Passthru ("./ppmtogif hunte. ppm ");

2) use the popen () function to open the process

The preceding method can only execute commands, but cannot interact with commands. However, sometimes you must enter something into the command. For example, when adding a Linux system user, you must call su to change the current user to the root user, the su command must enter the root password on the command line. In this case, it is obviously not feasible to use the method mentioned above.
The popen () function opens a Process Pipeline to execute the given command and returns a file handle. Since a file handle is returned, you can read and write it. In PHP3, you can only perform a single operation mode on the handle, either write or read. From PHP4, you can read and write the handle at the same time. Unless this handle is opened in a mode (read or write), you must call the pclose () function to close it.

Example 1:
$ Fp = popen ("/bin/ls-l", "r ");

Example 2:
/* Add a system user in PHP
The following is a routine. Add a user named james,
The root password is verygood. For reference only

The code is as follows: Copy code
*/
$ Sucommand = "su -- login root -- command ";
$ Useradd = "useradd ";
$ Rootpasswd = "verygood ";
$ User = "james ";
$ User_add = sprintf ("% s" % s "", $ sucommand, $ useradd, $ user );
$ Fp = @ popen ($ user_add, "w ");
@ Fputs ($ fp, $ rootpasswd );
@ Pclose ($ fp );
?>

3) use the reverse marker (', that is, the one under the ESC key on the keyboard, and ~ )
This method was not included in the document of PHP before and existed as a secret. The method is very simple. We use two anti-apostrophes to enclose the command to be executed as an expression. The value of this expression is the result of command execution. For example:
$ Res = '/bin/ls-l ';
Echo'
'. $ Res .'
';

The output of this script is like:
Hunte.gif
Hunte. ppm
Jpg.htm
Jpg.jpg
Passthru. php


Sample program

This example is easier to understand when executed once. (create a directory, put two files at will, and then place the program for execution)

The code is as follows: Copy code

<? Php
Echo "nsystem ";
$ Last_line = system ('Ls', $ return_var );
Echo "nreturn_var :";
Print_r ($ return_var );
Echo "nlast_line :";
Print_r ($ last_line );

Echo "nnexec ";
Exec ('Ls', $ output, $ return_var );
Echo "nreturn_var :";
Print_r ($ return_var );
Echo "noutput :";
Print_r ($ output );

Echo "nnshell_exec ";
$ Output = shell_exec ('Ls ');
Echo "noutput :";
Print_r ($ output );
?>

*/

 

?>

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.