D
dman_2007
Guest
If you ever wanted to run an external command / program from inside your PHP script, PHP provides following ways to do it :
1) backticks operator
You can use `` operators. Note that these are not same as apostrophes (single quotes) ''. Generally backtick is located on the key as ~ (tilde). Whenever you use backticks operator, PHP will try to run the content contained within backticks as a shell command from current directory and the command output will be returned which can be collected by using an assignment operator. For example, after running following code :
$file_list variable will contain the complete command output.
2) exec function
exec function can also be used for running external commands. We pass the shell command as first parameter. We also need to pass two more variable references as parameters, exec function fills them up with command output and command return status value respectively. Variable containing program output is an array which contains each line of command output as separate array element. The exec function itself returns the last line of command output. For example, when we run this code :
$last_line variable will contain last line of command output. $file_list variable will be an array containing each line of command output as a separate element. $return_code variable will contain command return status value.
3) shell_exec function
shell_exec is similar to backticks operator, we need to pass it the external command as a parameter and it will return the complete command output. For example, after this code
$output variable will contain the complete command output.
4) system function
System function also takes the shell command to be run as its first parameter. Second parameter is a variable which passed as a reference to be filled up with command's return status value. Its different from shell_exec in the way it handles the command output. Instead of returning the complete command outoput, it only returns the last line of output. Another thing to note is that it autmatically sends the complete command output to the browser as well. For example, after running this code
$last_line variable will contain last line of command output. $return_value variable will contain command return status value. Complete command output will also be send to the browser by system command.
5) passthru function
passthru function is quite similar system function. It takes two parameters, first is the shell command to execute and second is a variable passed as reference used for collecting command return status value. passthru function doesn't returns any value. It sends the complete command output to the browser. This function is preferred over exec and system functions when running command which produce binary data output.
In my next post i will show you some other advanced methods as well. I will also discuss best practices for using these functions.
1) backticks operator
You can use `` operators. Note that these are not same as apostrophes (single quotes) ''. Generally backtick is located on the key as ~ (tilde). Whenever you use backticks operator, PHP will try to run the content contained within backticks as a shell command from current directory and the command output will be returned which can be collected by using an assignment operator. For example, after running following code :
Code:
$file_list = `ls -l`;
$file_list variable will contain the complete command output.
2) exec function
exec function can also be used for running external commands. We pass the shell command as first parameter. We also need to pass two more variable references as parameters, exec function fills them up with command output and command return status value respectively. Variable containing program output is an array which contains each line of command output as separate array element. The exec function itself returns the last line of command output. For example, when we run this code :
Code:
$last_line = exec('ls -l', $file_list, $return_code);
$last_line variable will contain last line of command output. $file_list variable will be an array containing each line of command output as a separate element. $return_code variable will contain command return status value.
3) shell_exec function
shell_exec is similar to backticks operator, we need to pass it the external command as a parameter and it will return the complete command output. For example, after this code
PHP:
$output = shell_exec('ls -l');
$output variable will contain the complete command output.
4) system function
System function also takes the shell command to be run as its first parameter. Second parameter is a variable which passed as a reference to be filled up with command's return status value. Its different from shell_exec in the way it handles the command output. Instead of returning the complete command outoput, it only returns the last line of output. Another thing to note is that it autmatically sends the complete command output to the browser as well. For example, after running this code
Code:
$last_line = system('ls -l', $return_value);
$last_line variable will contain last line of command output. $return_value variable will contain command return status value. Complete command output will also be send to the browser by system command.
5) passthru function
passthru function is quite similar system function. It takes two parameters, first is the shell command to execute and second is a variable passed as reference used for collecting command return status value. passthru function doesn't returns any value. It sends the complete command output to the browser. This function is preferred over exec and system functions when running command which produce binary data output.
In my next post i will show you some other advanced methods as well. I will also discuss best practices for using these functions.