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:
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:
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: