There are several common functions for PHP to execute system commands

Source: Internet
Author: User

PHP executes system commands have several commonly used functions, such as: System functions, EXEC functions, popen functions, passthru,shell_exec functions They can execute system commands, but the premise must be the system to give permission Oh.

System functions

Description: Executes external program and displays output data.

Syntax: string system (String command, int [Return_var]);

return value: String

In detail: This function is like the C language of the function system (), used to execute the instruction, and output the result, if the Return_var parameter exists, then the execution of the command after the state will be filled in return_var, it is also worth noting that if you need to process the user input data, But also to prevent users to hack the system, you can use Escapeshellcmd (), if PHP in a modular execution, this function will automatically update the WEB server output buffer staging area after each line output, if you need a complete return string, and do not want to go through unnecessary other intermediate output interface, You can use PassThru ().

Instance code:

  1. echo "Last line of the output:".  $last _line; echo "
  2. ?>

exec function

Description: Executes an external program.

Syntax: string exec (String command, string [array], int [return_var]);

return value: String

Details: This function performs an external program or external instruction for the input command. Its return string is just the last line returned after the external program executes, and the PassThru () function can be used if a complete return string is required.

If the parameter array is present, command will add the array to the parameter, and if you do not want the array to be processed, you can call unset () before executing exec (). If Return_var and array two parameters are present, the state after the command is executed is populated with Return_var.

It is worth noting that if you need to deal with user input data, but also to prevent users to hack the system, you can use Escapeshellcmd ().

Instance code:

  1. < php echo exec ("WhoAmI");
  2. ?>

Popen function

Description: Opens the file.

Syntax: int popen (String command, string mode);

return value: Integer

Details: This function executes the instruction file, and the file is processed by pipeline. Files opened with this function can only be one-way (read-only or write-only) and must be closed with pclose (). You can use Fgets (), FGETSS (), and fputs () on file operations. Returns the False value if an error occurs on the file.

Instance code:

  1. <? $fp = Popen ("/bin/ls", "R");
  2. ?>

Executing system external commands using function PassThru

Prototype: function PassThru (string $command, int[optional] $return _value)

Knowledge Points: The difference between PassThru and system, passthru directly outputs the result to the viewer, does not return any values, and it can output binary, image data.

reverse apostrophe ' (and ~ in the same key) executes the system external command, the code is as follows: Echo ' dir ';

Knowledge Points: When you use this method to execute system external commands, you want to make sure that the Shell_exec function is available, otherwise the system external commands cannot be executed using this type of anti-apostrophe.

Security Note: When you use these functions to execute commands, you need to consider system security if you are committing the data according to user submissions, and you can use the Escapeshellcmd () and Escapeshellarg () functions to prevent malicious users from executing commands on the system. Escapeshellcmd () is for system commands executed, while Escapeshellarg () is for parameters that execute system commands. These two parameters are somewhat similar to the functionality of Addslashes ().

Now let's look at a custom function that executes the system command, with the following code:

  1. function Execute ($CFE) {$res = ";
  2. if ($CFE) {if (function_exists (' system ')) {
  3. @ob_start (); @system ($CFE);
  4. $res = @ob_get_contents (); @ob_end_clean ();
  5. } elseif (Function_exists (' PassThru ')) {@ob_start ();
  6. @passthru ($CFE); $res = @ob_get_contents ();
  7. @ob_end_clean (); } elseif (Function_exists (' shell_exec ')) {
  8. $res = @shell_exec ($CFE); } elseif (function_exists (' exec ')) {
  9. @exec ($cfe, $res); $res = Join ("n", $res);
  10. } elseif (@is_resource ($f = @popen ($cfe, "R"))) {$res = ";
  11. while ([email protected] ($f)) {$res. = @fread ($f, 1024);
  12. } @pclose ($f);
  13. }  }
  14. return $res; }

There are several common functions for PHP to execute system commands

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.