Php function shell exec

PHP Shell_exec: Learn To Run Shell Commands From Your Script

Position Is Everything

Php shell command

The PHP shell_exec function is the best choice to execute the commands through the shell. Indeed, it is not the only one because PHP has the exec(), system(), and passthru() functions as well. Therefore, in this article, you’ll learn about the shell_exec() function, and see how it’s different from the exec(), system(), and passthru() function.

Don’t skip reading to discover some amazing facts and practical ways to work with the shell commands in PHP.

What is Shell_exec PHP?

The PHP shell_exec is a built-in function of PHP that can be used to execute or run commands through the shell. It accepts only a single “command” argument and returns the entire output of the given command as a string.

PHP Shell Command Examples

The shell syntax looks like this: shell_exec(command) . However, you’ll get NULL when either the command returns nothing or an error occurs. Moreover, false and a warning are returned on the failed establishment of the pipe.

You need to enable the PHP shell_exec() function in the php.ini file before using it in your program. It is because the given function is mostly disabled, specifically when you are using PHP in safe mode.

You can PHP execute shell command from the PHP script by calling the PHP shell_exec function with the required command. Next, you’ll echo the result returned by the given function to see the output of the command on your screen.

For instance, you want to get the list of files present in your current directory. Therefore, you’ll need to execute the PHP shell_exec function by passing “ls” as an argument to receive your desired output. Later, you’ll print the output by using the echo statement.

Surprisingly, it isn’t illegal to copy and paste the below code script in your code editor to execute the “ls” command:

// using the shell_exec function with ls command
$list = shell_exec(“ls”);
// printing the entire output of the command
// as a string
echo “
$list

”;
?>

What is the Difference Between Shell_exec() and Exec()

The difference between the PHP shell_exec and exec() function is that the shell_exec() function returns the whole output of the given command. On the other hand, the exec() function gives back only the last line of the entire output.

Besides the above difference, another dissimilarity lies in the parameters of both of the given functions. Unlike the PHP shell_exec function, the exec() function accepts three parameters including the “command”, “output”, and “result_code.” Moreover, the exec() function stores all the lines returned by the command in an array represented by the second argument. Also, the status of the command execution is saved in the “result_code.”

Furthermore, you’ll notice that the exec() function returns NULL only if the given command doesn’t run perfectly.

– Coding Example

Suppose you want to PHP run shell script “ls -R” to get all the files along with the subdirectories of your current directory. As the exec() function returns only the last line of the output, you’ll specify an array as the second argument to collect all the lines one after the other. Next, you’ll use the “result_code” to see if the given command was successfully executed.

Please don’t hesitate to use the below code snippet for comparing the exec() and PHP shell_exec:

// using the shell_exec function with ls -R command
$string1 = shell_exec(“ls -R”);
// printing the output of shell_exec
echo “
$string1

”;
// using the exec() function and storing the output in an arrray
exec(‘ls -R’, $output, $result_code);
// printing the output array
print_r($output);
echo “
”;
// iterating through the output array
foreach($output as $element)
<
// printing each element value on a separate line
echo $element.”
”;
>
// printing the result code
echo $result_code;
?>

Shell_exec() Versus System() Function

The difference between the PHP shell exec and system() function is that the system() function isn’t dependent on the developer to display the result after executing a specific command. Whereas, the result of the PHP shell_exec needs to be printed individually.

Moreover, you’ll see that the system() function returns only the last line of the output similar to the exec() function stated above. Also, the number of parameters differs between the PHP shell exec and system() functions in a way that the latter asks for two parameters including the command and the result code.

Furthermore, the system() function simply returns false on failure unlike the PHP shell exec function that returns false and a warning.

– Coding Example

For instance: you are given a task to immediately print the last line from the output of the “ls -a” command. Therefore, you’ll execute the system() function by passing the “ls -a” command and the $result_code as arguments. Eventually, you’ll get the desired last line of output.

Please see the following code fragment to get a clear idea regarding the example stated above:

// using the shell_exec() function
shell_exec(“ls -a”);
// using the system() function
system(“ls -a”, $result_code);
?>

After running the above code, you won’t see any visible result by executing the PHP shell_exec function. However, the second code line will print the last line from the output of the “ls -a” command.

Differences Between PHP Shell Exec and Passthru()

The difference between the PHP shell exec and passthru() function is that the passthru() function deals with the binary data returned by the given command whereas the PHP shell exec function deals with the returned text. Moreover, the passthru() prints the raw data immediately on your screen.

Along with this, the given function also accepts a command and the $result_code as arguments which goes contrary to the PHP shell exec function. Also, the passthru() function returns nothing.

– Coding Example

Imagine that you want to PHP run shell command that returns an image. Therefore, you have set the Content-Type to image/jpg. But remember that in such a scenario the PHP shell exec function won’t help you because you are trying to get binary data. Hence, you’ll use the passthru() function by passing the required command and the $result_code as arguments. Consequently, you’ll get the image displayed without any error.

Here is the code block that runs both the given functions to let you see the difference:

// setting the content type
header(‘Content-Type: image/jpg’);
// executing the shell_exec function
shell_exec(“yourCommand”);
// executing the passthru function
passthru(“yourCommand”, $result_code);
?>

Why PHP Shell Exec Isn’t Working on Windows?

You can run the PHP shell exec successfully on Windows by appending “ 2>&1” to your required command. It will ensure that the output of the given command is redirected to the right place. Here is the function implementation that works fine “shell_exec(“command 2>&1”).

The string “ 2>&1” will ensure that the output of the given command is redirected to the right location. Surprisingly, you can append the same string while using the exec() and system() functions in case you aren’t getting the output.

– Coding Example

Let’s assume you are executing the “tail” command by using the PHP shell exec function. But you aren’t getting the output. So, you’ll add “ 2>&1” at the end of the command and reload your script. Eventually, you’ll get the command output redirected correctly.

Here is the code snippet that the above example is talking about:

$get = shell_exec(“tail 2>&1”);
echo “
$get

”;
?>

How to Stop Passthru() From Directly Printing the Output

You can use the ob_get_contents() function after the passthru() function to get the output of the command. In this way, the binary data won’t be displayed on the screen. Moreover, don’t forget to execute the ob_start() at the beginning and the ob_end_clean() function after the ob_get_contents() function.

Here is the coding steps for your reference:

  1. Run the ob_start() function
  2. Execute the passthru() function with your required command
  3. Run the ob_get_contents() function like “variable1 = ob_get_contents()”
  4. Execute the ob_end_clean() function

Conclusion

Summarizing the above discussion, you have got enough knowledge regarding the PHP shell_exec function along with some other functions created for a similar purpose. Listed below are the points that you should remember for successfully executing the commands from your PHP script:

  • The PHP shell_exec function is used to execute the commands through the shell from your PHP script
  • The PHP shell_exec function accepts a single command and returns the entire output of the command in the form of a string
  • You can use the exec() function to execute a particular command and get only the last line of the output.
  • You can opt for the system() function to run a command and instantly print the last line of the command output
  • The passthru() function proves to be helpful when you want to execute a command that returns binary data such as images

What is shell exec php

Now, you know all the important functions for executing the commands. Choose the command that you want to execute, have a look at your output requirements, and execute the function that fits the best.

Источник

PHP shell_exec Function: How to Use It [With Examples]

PHP shell exec Function

This tutorial explains how to use the shell_exec function in PHP in order to execute code via the shell and return the output as a string.

PHP is a versatile programming language for building server-side web applications, but sometimes you need to execute code from another environment and use the result in PHP.

This can be achieved by using the shell_exec function to execute commands on the system’s shell hosting your PHP code and return the result as a string to PHP.

When doing so, there are security considerations – never pass user input to shell_exec directly! – otherwise, you’re just giving third parties carte blanche to execute whatever code they like directly on your server.

shell_exec Syntax

Оцените статью