PHP executes a system command with several common functions _php tutorial

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

Detailed Description:
This function is like the function system () in C, which executes the instruction and outputs the result. If the Return_var parameter exists, the state after the command is executed is populated with Return_var. It is also 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 (). If PHP is executed in a modular style, this function automatically updates the output buffer staging area of the WEB server after each line output. If you need a complete return string and do not want to go through an unnecessary other intermediate output interface, you can use PassThru ().

Instance code:

The code is as follows Copy Code

< PHP
$last _line = System ("ls", $retval);
echo "Last line of the output:". $last _line;
echo "

Return value: ". $retval;
?>

exec function
Description: Executes an external program.
Syntax: string exec (String command, string [array], int [return_var]);
return value: String

Detailed Description:
This function executes an external program or external instruction that enters the 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:

The code is as follows Copy Code

< PHP
echo exec ("WhoAmI");
?>

Popen function
Description: Opens the file.
Syntax: int popen (String command, string mode);
return value: Integer

Detailed Description:
This function executes the instruction file, and the file is processed in a pipe manner. 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:

The code is as follows Copy Code

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


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.

Method Four: Reverse apostrophe ' (and ~ in the same key) Execute system external command

The code is as follows Copy Code

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 instructions

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 look at a custom function for executing system commands

The code is as follows Copy Code

function Execute ($CFE) {
$res = ";
if ($CFE) {
if (function_exists (' system ')) {
@ob_start ();
@system ($CFE);
$res = @ob_get_contents ();
@ob_end_clean ();
} elseif (Function_exists (' PassThru ')) {
@ob_start ();
@passthru ($CFE);
$res = @ob_get_contents ();
@ob_end_clean ();
} elseif (Function_exists (' shell_exec ')) {
$res = @shell_exec ($CFE);
} elseif (function_exists (' exec ')) {
@exec ($cfe, $res);
$res = Join ("n", $res);
} elseif (@is_resource ($f = @popen ($cfe, "R")) {
$res = ";
while (! @feof ($f)) {
$res. = @fread ($f, 1024);
}
@pclose ($f);
}
}
return $res;
}

http://www.bkjia.com/PHPjc/631536.html www.bkjia.com true http://www.bkjia.com/PHPjc/631536.html techarticle PHP executes system commands with several common functions, such as: system function, exec function, popen function, passthru,shell_exec function They can all execute system command, but the precondition must be system ...

  • Related Article

    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.