Skip to main content

PHP Exec Function To Execute Command via Code

Apart from creating a user based application, you can also develop system or server management application. In system or server management application lots of time, you need to execute an external program. To execute the external programs PHP has different functions. PHP Exec is one of the functions which can be used to execute any external program. Using PHP exec function you can execute any external program. In this post, we will explore in-depth of php exec function.

PHP EXEC function detail

Signature Of Exec Function:
string exec(string Command, [array &Output, int &Return_Var])

Input Parameters:
Followings are input parameters of the php exec function:

  1. Command: The command of the external program which you want to execute, it will be in the form of a┬ástring like for listing the files in the┬ádirectory the parameter will be “ls -al /Applications/XAMPP/xamppfiles“. This is a mendatory parameter.
  2. Output: An array passed by reference and non-mandatory parameter. If Output will be passed it will contain output of the command with every line as an array element. If the array will already have some value, the output will be appended to the array element.
  3. Return_Var: An integer passed by reference and it is the non-mandatory parameter. If Return_Var will be passed it will contain the status of the program

The output of PHP Exec Function: PHP exec function returns the last line of the output from the result of the command.

PHP Exec Function examples

Below is a simple example of PHP exec function.

$return_exec = exec('ls -al /Applications/XAMPP', $out , $return_var);
print "\nOutput in out\n";
print "\nOutput returnedby php exec function\n";
print $return_exec;
print "\nOutput written in return_var \n";
print $return_var;

In the above example code at line #1 we are trying to run ls command which list the files and directory inside /Applications/XAMPP. We have alos passed $out and $return_var which are passed by reference and after we have called function exec now $out and $return_var has some value. So below is the output of above program on my computer.
PHP Exec Example

PHP Exec and Output Streaming

Some of the external commands can have long tail output while running the command. Also, these type of command runs for a longer period of time to accomplish their job. But if you will run such type of command directly using the exec function then it might hang your output because the web server(may be apache) have a timeout or maybe your script will have max execution time. So to avoid such situation you put that command in a background process and trap the output in any text file. Also on user side on the screen refresh the output text file.

Let us take a very small example of curl command using exec function:
exec('curl -v -o /Applications/XAMPP/xamppfiles/htdocs/techflirt/exec/ab.txt > /dev/null &', $ope, $a);
ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
while (@ob_end_flush());
ini_set('implicit_flush', true);
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
echo '<pre>';
$handle = fopen('/Applications/XAMPP/xamppfiles/htdocs/techflirt/exec/ab.txt', "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line;

In the above code we are using exec function and putting the command in the background and taking output into a text file. Not the same text file is being streamed on browser in prograssive manner.

PHP Exec and User Handling

Program on any operating system execute on the user basis. PHP can be executed either via apache httpd process or command line tool. So if you are using exec function then be very cousicious about from where you are running the command. If you are running your script from web then the programm command passed in exec function should be allowed to be executed via your apache user. If you are running your php script via command line then the same user by which you are loggedin should have enought rights to execute the same command which you have passed in exec().


To see which user the program is going to be run just first echo exec(‘whoami’).

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

2 thoughts to “PHP Exec Function To Execute Command via Code”

  1. Directly no but you can have workarround by first taking the command output in file and reading the file and show the content like below:
    exec(“ls => /var/a.txt”);
    $output = file_get_content(“/var/a.txt”);

Leave a comment/Ask Question