Call system commands in PHP with Exec, system, and other functions
Source: Internet
Author: User
KeywordsCall system commands in PHP with Exec system and other functions
Can execute the system command of the ASP original send
Http://tech.cncms.com/web/asp/21739.html
PHP's built-in function Exec,system can call system commands (shell commands) and, of course, passthru,escapeshellcmd functions.
In many cases, using PHP's Exec,system and other functions to call system commands can help us do our job better and faster. For example, the first two days of the author in bulk processing. rar files when exec helped me a lot.
Today, tidy up the common call system functions sent out to share experience with you.
Note: In order to use these two functions, the security mode in php.ini must be turned off, otherwise PHP is not allowed to invoke system commands for security reasons.
Let's take a look at the explanations of these two functions in the PHP manual:
EXEC () Executes the command given, but it does not output anything, it simply returns the last line from the result of the command, and if you need to execute a command and get all the data from the command, you can use the PassThru () function.
If a parameter array is given, the specified array will be filled with each line output by the command, note that if the array already contains some elements, exec () will append it to the array, and if you do not want this function to append elements, you can pass this array to exec () Before calling Unset ().
If there is a given parameter array and Return_var, then the return execution Status command will be written to this variable.
Note: If you allow data input from the user to be passed to this function, then you should use Escapeshellcmd () to determine that the user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it in the background (background) execution, you must make sure that the output of the program is redirected to a file or some output stream, otherwise PHP will hang Until the end of the program execution.
The system---Executes the external program and displays the output
Syntax: string system (String command [, int &return_var])
Description
System () executes the command given and outputs the result. If a parameter return_var is given, the execution of the command's status code will be written to this variable.
Note: If you allow data input from the user to be passed to this function, then you should use Escapeshellcmd () to determine that the user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it in the background (background) execution, you must make sure that the output of the program is redirected to a file or some output stream, otherwise PHP will hang Until the end of the program execution.
If PHP is operating as a server module, after each line is output, system () tries to automatically clear the output buffer of the Web server.
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.