Execute Shell Command in PHP using exec()

The PHP script is mainly used for developing web applications but it can be used for other purposes also. PHP has some built-in functions to execute system-related commands. exec() is one of them. It is used to execute shell commands or any program from the PHP script. How this function can be used in PHP are shown in this tutorial.

Syntax:

string exec ( string $command [, array &$output [, int &$return_var ]] )

This function can take three arguments. The first argument is mandatory that will take the system command. The other two arguments are optional. The second argument is used to store the output of the command in an array. The third argument of this function is used to store the return status of the executed command. This function returns the last line from the executed command output.

Example-1: Use of exec() function without optional arguments

The basic use of the exec() function has shown in this tutorial. Create a PHP file with the following script to know how the exec() function returns the command output.  ‘pwd‘ command has used in the first exec()command of the script that returns one line of output. ‘ls -la‘ command has been used in the second exec() command that can return multiple lines of output. If any command returns multiple lines then the output will show the last line as the output.

<?php

//Store the output of the executed command

$output1 =  exec('pwd');

//Print the return value

echo $output1;

echo "<br/>";

//Store the last line of the executed command

$output2 = exec('ls -la');

//Print the return value

echo $output2;

?>

Output:

The following output will appear after running the above script from the server. ‘pwd‘ command returns the current working directory as output that is shown in the first output. ‘ls -la‘ command returns the details information of the list of directories and the second output shows the last line from the command output.

Example-2: Print all values of the executed command

In the previous example, no optional argument is used in the exec() function. The following example shows the use of optional arguments of the exec() function. Create a PHP file with the following script. Two optional arguments of exec() are used in this script. ‘ls -l‘ command is used in the first argument that returns the list of directories. $output variable is used here to store the output of the command in an array. $status variable is used to store the return status value of the executed command. The output of the command will be printed as an array and each value of the output array will be printed by using the ‘for’ loop.

<?php

//Store the output of the executed command in an array

exec('ls -l', $output, $status);


//Print all return values of the executed command as array

print_r($output);

echo "<br/>";

//Print the output of the executed command in each line

foreach($output as $value)

{


        echo $value."<br />";

}

//Print the return status of the executed command

echo $status;

?>

Output:

The following output will appear after running the above script from the server. The output shows the array that contains the output of the command, ‘ls -l’ and each value of the array in each line.

Example-3: Print all PHP files of the current directory

The following example shows the list of all PHP files of the current directory by using the exec() function. Here, the ‘ls -l *.php‘ command is used here to find out the list of all PHP files of the current directory. <pre> tag is used in the script to print the content of the array with the structured format.

<?php

//Store the output of the executed command in an array

exec('ls -l *.php', $output);

//Print the output of the executed command

echo "<pre>";

print_r($output);

echo "</pre>";

?>

Output:

The following output will appear after running the above script from the server.

Example-4: Run a bash script

How any bash script can be executed by using the exec() function has shown in this example. Create a bash file named loop.sh with the following script that will print all even numbers from 1 to 20.

loop.sh

#!/bin/bash

#Initialize the counter

counter=1

#Iterate the loop until the $counter value is less than or equal to 20

while [ $counter -le 20 ]

do

#Print the even numbers

if [[ $counter%2 -eq 0 ]]

then

#Print $counter without newline

echo  "$counter"

fi

#Increment $counter by 1

((counter++))

done

Create a PHP file with the following script to run the bash script. ‘bash loop.sh‘ is used as the first argument of the exec() function that will execute the loop.sh script file. ‘foreach‘ loop is used to print each value of the $output with space.

<?php

//Run the bash script

exec('bash loop.sh', $output);


echo "All even numbers within 1-20 are:<br />";


//Print the ourput using loop

foreach($output as $value)

{


        echo $value."  ";

}

?>

Output:

The following output will appear after running the above script from the server. The output shows all even numbers within 1 to 20.

Example-5 : Run `dir` command using exec() function

‘dir’ command works like the ‘ls’ command. The following example shows how the ‘dir’ command can be executed using a PHP script. Create a PHP file with the following script that stores the output of the ‘dir’ command in the array named $output and the status value in the variable named $return. var_dump() function is used here to print the structure of the $output array with the data type information.

<?php

    //Run `dir` command

    exec("dir", $output, $return);

    //Print the return status value

    echo "The return value of the `dir` command is $return\n";

    echo "<pre>";

    //Print the command output

    var_dump($output);

    echo "</pre>";

?>

Output:

The following output will appear after running the above script from the server.

Conclusion:

Different uses of the exec() function have been explained in this tutorial to help the PHP coders to know the way to execute the shell command by using the PHP script. Some other functions also exist in PHP to do the same type of task.



from Linux Hint https://ift.tt/3arYUcr

Post a Comment

0 Comments