Home > Backend Development > PHP Tutorial > PHP executes linux system commands_PHP tutorial

PHP executes linux system commands_PHP tutorial

WBOY
Release: 2016-07-21 14:52:32
Original
1211 people have browsed it

First of all, I would like to introduce to you several basic functions of PHP to execute Linux system commands. For a long time, I couldn't tell the difference between the specific usage of the following functions.

system function
Description: Execute external program and display output data.
Syntax: string system(string command, int [return_var]);
Return value: string

Detailed introduction:
This function is like the function system() in C language, which is used to execute instructions and output the results. If the return_var parameter exists, the status after executing the command will be filled in return_var. It is also worth noting that if you need to process the data entered by the user, but want to prevent the user from playing tricks to crack the system, you can use EscapeShellCmd(). If PHP is executed in a modular fashion, this function will automatically update the output buffer buffer of the web server after each line of output. If you need to return a complete string and don't want to go through unnecessary other intermediate output interfaces, you can use PassThru().

Example code:

< ?php
$last_line = system('ls', $retval);
echo 'Last line of the output: ' . $last_line;
echo '
Return value: ' . $retval;
?>


exec function
Description: Execute external program.
Syntax: string exec(string command, string [array], int [return_var]);
Return value: string

Detailed introduction:
This function executes the external program or external instruction input command. Its return string is only the last line returned after the external program is executed; if you need a complete return string, you can use the PassThru() function.

If the parameter array exists, the command will add the array to the parameter for execution. If you do not want the array to be processed, you can call unset() before executing exec(). If both return_var and array parameters exist, the status after executing the command will be filled in return_var.

It is worth noting that if you need to process the data entered by the user and prevent the user from playing tricks to crack the system, you can use EscapeShellCmd().

Example code:

echo exec('whoami');
?>


popen function
Description: Open the file.
Syntax: int popen(string command, string mode);
Return value: integer

Detailed introduction:
This function executes the command to open a 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(). For file operations, fgets(), fgetss(), and fputs() can be used. If an error occurs while opening the file, a false value is returned.

Example code:

$fp = popen( "/bin/ls", "r" );
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371565.htmlTechArticleFirst of all, I will introduce to you several basic functions of PHP to execute Linux system commands. For a long time, I couldn't tell the difference between the specific usage of the following functions. system function description: execute...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template