Shell Commands PHP

This section describes functions that interact with the command shell in some way. Some of them execute other programs, and two of them read or write to environment variables.

string exec(string command, array output, integer return)
The exec function attempts to execute the command argument as if you had typed it in command shell. Nothing is echoed to the browser, but the last line of output from the execution is returned. If the optional output argument is supplied, each line of output will be added to the output argument as an array element. If the optional return argument is supplied, the variable is set to the return value of the command.

It is very dangerous to put any user-supplied information inside the command argument. Users may pass values in form fields that allow them to execute their own commands on your Web server. If you must execute a command based on user input, pass the information through the escape-shellcmd function.
Compare this function to passthru and system.

<?
// get directory list for the root of C drive
$LastLine = exec("dir C:\ quote>,$AllOutput, $ReturnValue); print("Last Line:$LastLine BR>\n");
print("All Output:BR>\n");
for($index = 0;$index count($AllOutput);$index++)
{
print("$AllOutput[$index] BR>\n");
}
print("BR>BR>\n");
print("Return Value: \$ReturnValueBR>\n");
?>

string getenv(string variable)
The getenv function returns the value of the given environment variable or false if there is an error. PHP converts all environment variables into PHP variables, so this function is useful only in those rare instances when environment variables change after a script begins executing. If you need to set the value of an environment variable, use putenv.

<?
print(getenv("PATH"));
?>

string passthru(string command, integer return)
The passthru function is similar to exec and system. The command argument is executed as if you typed it in a command shell. If you provide the optional return argument, it will be set with the return value of the command. All output will be returned by the passthru function and sent to the browser. The output will be sent as binary data. This is useful in situations where you need to execute a shell command that creates some binary file, such as an image.

It is very dangerous to put any user-supplied information inside the command argument. Users may pass values in form fields that allow them to execute their own commands on your Web server. If you must allow this, pass the information through the escapeshellcmd function first.

putenv(string variable)
The putenv function sets the value of an environment variable. You must use syntax similar to that used by a command shell, as shown in the example below. To get the value of an environment variable, use getenv, or use phpinfo to dump all environment variables.

<?
putenv("PATH=/local/bin;.");
?>

string system(string command, integer return)
The system function behaves identically to C's system function. It executes the command argument, sends the output to the browser, and returns the last line of output. If the return argument is provided, it is set with the return value of the command. If you do not wish for the output to be sent to the browser, use the exec function.

It is very dangerous to put any user-supplied information inside the command argument. Users may pass values in form fields that allow them to execute their own commands on your Web server. If you must allow this, pass the information through the escapeshellcmd function first.

<?
// list files in directory
print("PRE>");
system("ls -l");
print("/PRE>");
?>