How To Execute Shell Commands with PHP Exec and Examples


Php provides web-based functionalities to develop web applications. But it also provides system related scripting and execution features. exec() function is used to execute an external binary or program from a PHP script or application. In this tutorial, we will look at different use cases and examples of exec() function like return value, stderr, shell_exec, etc.

exec() Function Syntax

The PHP exec()syntax is like below where single parameter is mandatory and other parameters are optional.

exec(string COMMAND, array OUTPUT, int RETURN_VARIABLE);

COMMAND is the command we want to execute with exec() function. The command should be a string value or variable. COMMAND is a mandatory parameter.

OUTPUT is the output of the COMMAND execution. OUTPUT is an array that can hold multiple values or lines. OUTPUT is optional where it can be omitted.

RETURN_VARIABLE is the return value of the given COMMAND. RETURN _VALUE is generally the process status of the command. RETURN_VALUE is an integer and optional to use.

Run External System Binary or Application

We will start with a simple example. We will provide the command we want to run on the local operating system. In this example, we will create a directory named data. This directory will be created in the current working path. We can also specify the path explicitly like /var/data.

 exec("mkdir data");

We can list the created directory with Linux file command like below. We will also provide the directory name because exec() function will only show the last line of the output. We will use echo to print the command output.

echo exec("file data");
Run External System Binary or Application
Run External System Binary or Application

Print Output

We have already looked at printing output but I want to explain more about printing output. exec() function output or return can be printed with echo but the printed part will be only last line. So in a multi-line output, we can not see the output with echo. If we want to see the whole output we should use the system() function which will be explained below.

But we can use the output parameter like below. In this example, we will put command output to the o. The output parameter is in array type so we will use print_r to print output.

exec("ls",$o);
print_r($o);
PHP exec Output

Assign Return Value into Variable

Using echo is not a reliable way to get the return value. We can use variables to set return value and use whatever we want. Int this example we will set the process return value to v variable.

exec("ls",$o,$v);
echo $v;
Assign Return Value into Variable
Assign Return Value into Variable

Return Complete Output as String with shell_exec()

PHP language provides different functions as an alternative to exec(). We can use the shell_exec() function which will create a shell process and run given command. In this example, we will look at ls command and print output. With the shell_exec() function we can not get the return value of the shell process or command.

echo shell_exec('ls');
Return Complete Output as String with shell_exec()
Return Complete Output as String with shell_exec()

PHP shell_exec() Examples

In this part, we will make more examples about the PHP shell_exec() function. We will run different system and Linux commands like date , whoami , ifconfig and mkdir .

> echo shell_exec('date');

> echo shell_exec('whoami');

> echo shell_exec('ifconfig');
PHP shell_exec() Examples

Return Complete Output as String with system()

Another similar function is system(). system() function displays output directly without using echo or print. In this example, we will run the ls command again.

 system('ls');
Return Complete Output as String with system()
Return Complete Output as String with system()

exec() Function vs shell_exec() Function

PHP also provides the shell_exec() function which is very similar to the exec() function. The main difference is shell_exec() function accept single parameter which is command and returns the output as string.

//Execude command in the shell with PHP exec() function
//put the output to the $output variable 
exec("uname -a",$output,$return_val);

print_r($output);

//Execude command in the shell with PHP shell_exec() function
//put the output into $out variable
$out = shell_exec("uname -a");

echo $out;
exec() Function vs shel_exec() Function
exec() Function vs shel_exec() Function

LEARN MORE  Display Detailed System Information With Systeminfo For Windows Operating Systems

3 thoughts on “How To Execute Shell Commands with PHP Exec and Examples”

  1. Commands like ls work fine but creating a directory in not working for me not sure if I need to change any permissions or anything.
    Any idea?

    Reply
    • Almost certainly some kind of permissions problem. The user running the PHP script needs to have the ability to do what the command wants to do. Try creating a directory in /tmp and see if that works. Make sure your command is /bin/sh compatible since that’s hardcoded into PHP.

      Reply
    • late but… write the command with path for success:
      exec(“/usr/bin/mkdir dir”,$output,$return_val);

      Reply

Leave a Comment