How to Execute Shell Commands in PHP

There are times when you need to run a shell command from inside a PHP program. This can be for several reasons such as running terminal-based programs or running common terminal commands like ls. PHP can execute any shell command and return its output as a string or array if needed.

 

In this tutorial, we will learn how to execute shell commands in PHP with examples.

 

PHP exec() Function Syntax

 

exec(command, output, return_variable)

 

Here is what each parameter is used for in exec()

 

  • command - the only required argument in exec(). It is the terminal command to run
  • output - do something with the output of the command. Can be used to return the output back into the PHP program as a string
  • return_variable - put the output in the variable defined in this parameter

 

Make a Directory Using exec()

In this example, we will create a directory. We will do this using the Linux mkdir command and passing in the name of the directory we want to create.

 

~/sites/skillsugar/public/test.php
<?php
exec('mkdir test');

 

The above command will create a directory in the same directory as the PHP file that executed it.

 

~/sites/skillsugar/public
ls
test.php
test

 

To create a directory in a different location on the system pass in a path before the name of the directory. This can be an absolute path (relative to root) or a relative path to the location of your program.

 

exec('mkdir /var/www/sites/skillsugar/public/test');

 

Using a Variable inside exec()

It is possible to pass variables into exec() by closing the ' (single quote) or " (double quote) and concatenating the variable in using a . (dot).

 

$file_name = 'new_dir';
exec('mkdir /home/vagrant/sites/skillsugar/public/' . $file_name);

 

Another example:

 

$dir_name = 'new_dir';
exec('mkdir ' . $dir_name . '/file.txt');

 

Printing the Output from exec()

You can simply echo the output.

 

echo exec('ls');
web.config

 

Multi-line Output from exec()

You will notice that the above example only prints the last line from the ls command. To get all the lines we can set an output variable in the second parameter of exec() and then print the array containing all the lines.

 

exec('ls', $out);
print_r($out);
Array (
	[1] => build
	[2] => cgi-bin
	[3] => css
	[4] => error_log
	[5] => example.txt
	#...
	[16] => test
	[17] => test.php
	[18] => test2
	[20] => translations
	[21] => web.config
)

 

Set a Return Variable

To get what the terminal command returned we can set a variable in the third parameter of exec().

 

exec('ls', $out, $r);
echo $r;
0

 

Return Output as a String with the PHP shell_exec() Function

To return a complete string from a shell execute command in PHP use the shell_exec() function instead. It works in the same way to exec() except it takes no arguments and will return the complete output as a string.

 

echo shell_exec('ls');
build
cgi-bin
css
error_log
example.txt
#...
test
test.php
test2
translations
web.config

 

PHP system() Function

The system() PHP function works just like shell_exec() except you don't need to print or echo the output string as it does this for you.

 

system('ls');
build
cgi-bin
css
error_log
example.txt
#...
test
test.php
test2
translations
web.config

 

Conclusion

You now know how to execute a shell command in three different ways using PHP. exec() give you the most flexibility, but you will have to pass a second argument to get the complete output as a string. system() is the most basic way to execute a command and get the complete output but has limited functionality.

terminal shell command line