PHP » I/O » shell_exec()

Syntax:
string shell_exec(string command)
command
Command to be executed.

Executes a command.

shell_exec() executes a command in a shell and returns the output as a string. The command can also simply be placed within back-ticks (``).

Examples

Code:
<?php

$output = shell_exec("env");
print "<pre>$output</pre>";

?>
Output:
TERM=xterm
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
_=/bin/env
PWD=/var/www/html/samples
LANG=en_US.UTF-8
SHLVL=2
Explanation:

shell_exec() is used to list its environment variables.

Code:
<?php

$output =`ps`;
print "<pre>$output</pre>";

?>
Output:
PID TTY TIME CMD
2379 ? 00:00:00 httpd
2380 ? 00:00:01 httpd
2381 ? 00:00:00 httpd
2382 ? 00:00:00 httpd
2383 ? 00:00:00 httpd
2384 ? 00:00:01 httpd
2385 ? 00:00:00 httpd
2386 ? 00:00:00 httpd
15938 ? 00:00:00 ps
Explanation:

ps and back-ticks are used to print the running processes of the server user.

See Also: