Detailed implementation code for PHP execution of external programs

Source: Internet
Author: User
Tags call shell
This article mainly introduces the implementation of the PHP implementation of external programs, combined with examples of PHP to perform external programs commonly used functions related to the use of skills and considerations, the need for friends can refer to the following

The example in this paper describes how PHP implements external programs. Share to everyone for your reference, as follows:

In some special cases, the use of PHP invoke external program execution, such as: Call shell command, Shell script, executable program, and so on, today in the source of the code to understand the way PHP executes external programs, take this opportunity to tidy up a bit.

In the source code in the Exec.h file, a number of functions can be called external programs, the blue box of two functions is only a helper, this article only a few other functions are described.

Premise

1 PHP is not running in Safe mode, turn off safe mode, i.e.: Safe_mode = off

2 disabling function List disable_functions = exec, System, Shell_exec,proc_open, Popen, if disabled, turn it off.

Note: There is a risk of executing external programs, so use these functions in a secure situation.

EXEC () function

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

Description: The return value holds the final output, and all output will be saved to the $output array, $return _var the status code used to hold the command execution (to detect success or failure).

Example:


<?phpexec (' WhoAmI ', $output, $status); Var_dump ($output); exit;

Output Result:


Array (1) {[0]=> string (7) "Hedong"}

Attention:

The ① output is appended to the $output line by row, so you need to unset ($output) before calling exec, especially when looping calls.

② If you want to continue execution of subsequent code immediately after calling an external program via exec, simply adding "&" in the command is not enough, and exec will still wait for the command to complete, and you need to redirect the standard output, for example: Exec ("Ls-al >/dev/null & ", $output, $var);

Shell_exec () function

Prototype:string shell_exec( string command)

Description: Executes the command through the shell environment and returns the full output as a string.

Example:


<?php$output = shell_exec (' WhoAmI '); echo "$output"; Hedongexit;

Attention:

When an error occurs during process execution, or if the process does not produce output, NULL is returned, so using this function cannot detect whether the process executed successfully by the return value. If you need to check the exit codes for process execution, use the EXEC () function.

System () function

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

Description: Executes the given command, returns the final output, and the second parameter is optional to get the status code after the command executes.

Example:


<?phpsystem ("WhoAmI", $status); Direct output var_dump ($status); The status code is 0exit when successful;

Output Result: Hedong

PassThru () function

Prototype:void passthru (string command [, int return_var])

Description: Executes the given command, but does not return any output, but outputs it directly to the display device; the second parameter is optional to get the status code after the command executes.

Purpose: Use this function instead of the EXEC () or system () function when executing a UNIX command that outputs binary data and needs to be delivered directly to the browser

Example:


<?phppassthru ("WhoAmI", $status); Direct output var_dump ($status); The status code is 0exit when successful;

Output Result: Hedong

Popen () function

Prototype:resource popen ( string command, string mode )

Description: Opens a pipeline that points to a process that is generated by the execution of a derived command command. Returns the same file pointer that is returned by fopen (), except that it is one-way (read or write only) and must be closed with pclose (). This pointer can be used for fgets (), FGETSS (), and fwrite ().

Example:


$FD = popen ("command", ' R '); $ret = Fgets ($FD);

Note: You can only open one-way pipes, not ' R ' is ' W ', and you need to use Pclose () to close.

Proc_open () function

Prototype:resource proc_open ( string cmd, array descriptorspec, array &pipes [, string cwd [, array env [, array other_options]]] )

Description: Similar to Popen, but can provide bidirectional piping.

Example:


<?php/** * @author: Hedong * @date 2017-04-04 *///pipeline configuration $descriptors = Array (  0 = = Array ("Pipe", "R"),  1 => ; Array ("Pipe", "w")), $process = Proc_open ("php", $descriptors, $pipes), if (Is_resource ($process)) {  fwrite ($pipes [ 0], "<?php\n");  Fwrite ($pipes [0], "\ $rand = rand (+); \ n");  Fwrite ($pipes [0], "if (\ $rand = = 1) {\ n");  Fwrite ($pipes [0], "  echo \" Hello, world!\n\ "; \ n");  Fwrite ($pipes [0], "} else {");  Fwrite ($pipes [0], "  echo \" Goodbye, world!\n\ "; \ n");  Fwrite ($pipes [0], "}");  Fwrite ($pipes [0], "?>");  Fclose ($pipes [0]);  $output = "";  while (!feof ($pipes [1])) {    $output. = Fgets ($pipes [1]);  }  $output = Strtoupper ($output);  Echo $output; Fclose ($pipes [1]);  Proc_close ($process);}

Output Result:

GOODBYE, world!

Attention:

① need to close the resource using Proc_close (), and if it is a pipe type, you need to close the handle with Pclose ().
②proc_open Open Program as a child process of PHP, PHP quits after the child process will also exit.

Summarize:

The EXEC function stores the output results on the second parameter;

The Shell_exec function does not receive the return value in the parameter, and does not execute a successful status code;

The system function outputs the result of execution directly; the PassThru function differs from the system function in that it is suitable for processing output binary data;

The Popen function will fork a child process and return the file pointer

Proc_open function with Popen, but can provide bidirectional channel

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.