This post discusses how to execute shell commands via PHP. The ability to execute shell commands is a powerful feature and should be used carefully. As such, not all hosting providers will allow you to execute shell commands.
The PHP functions to execute shell command are: shell_exec(), exec() or system(). These functions are remarkably similar but have slight differences. Let’s take a look.
Table of Contents
shell_exec(): string
The shell_exec() function returns a string or NULL value. The returned string will contain the output of the command that you executed. However, the shell_exec() function will return a NULL if an error has occurred. You can also expect a NULL value if the command produces no output.
This command requires the following parameters:
- $cmd – Expects a string value that holds the command that you want to execute.
Next, let’s look at how you can use the shell_exec() function to return a directory listing of a Linux machine:
<?
//List the folders using ls
echo shell_exec("ls / -ltr");
This will output the following:
total 2097248 drwxr-xr-x 2 root root 4096 Apr 26 19:07 opt drwxr-xr-x 2 root root 4096 Apr 26 19:07 media drwxr-xr-x 2 root root 4096 Apr 26 19:07 lib64 lrwxrwxrwx 1 root root ….
exec(string $cmd, array &$output = null, int &$resultCode = null): string|false
The exec() function returns the last line of the executed command as a string. However, this command can also return FALSE if an error has occurred.
This command requires the following parameters:
- $cmd – Expects a string value that holds the command that you want to execute.
- &$output – Is an optional parameter that expects an array variable. The exec() function will update the variable with every line of output from the command. Trailing white space, such as \n, is not included in this array. Also, exec() will append to the end of the array if the array already contains items.
- &$resultCode – Is an optional parameter that expects an integer variable. The exec() function will update the variable with the exit status of the executed command.
Next, let’s look at how you can use the exec() function to return memory information of a Linux machine:
<?php
$output=null;
$resultCode=null;
//get the memory info of a Ubuntu machine
exec('free', $output, $resultCode);
echo "Returned with status $resultCode and output:\n";
print_r($output);
This will output the following:
Returned with status 0 and output:
Array
(
[0] => total used free shared buff/cache available
[1] => Mem: 16351256 9143564 283052 282788 6924640 6661072
[2] => Swap: 2097148 279644 1817504
)
system(string $cmd, int &$resultCode = null): string|false
The system() function is similar to the exec() function. It will however display output directly (without using echo() or print()).
This command requires the following parameters:
- $cmd – Expects a string value that holds the command that you want to execute.
- &$resultCode – Is an optional parameter that expects an integer variable. The exec() function will then update the variable with the exit status of the executed command.
Finally, let’s see how the system command is used using the exec() example from above:
<?php
$resultCode=null;
//get the memory info of a Ubuntu machine
system('free', $resultCode);
echo "Returned with status $resultCode and output:\n";
This will output the following:
total used free shared buff/cache available
Mem: 16351256 9246984 238044 307576 6866228 6531868
Swap: 2097148 285036 1812112
Returned with status 0 and output:
Wrapping up
It is easy to execute shell commands in PHP. Also, the shell_exec(), exec() or system() function must not be disabled in the php.ini file.
You may also be interested in
Updated:
- 2021-07-27 – Added information about disabling functions in the php.in.
Source:
Which one is secure?
how to write below command in php
net user userID /domain