Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is different between exec(), shell_exec, system() and passthru() functions in PHP? [duplicate]

Tags:

php

Anybody please tell me. I want know the different between the exec(), shell_exec, system() and passthru() function?

I search from php.net unable to get the answers I need.

like image 723
Developer Avatar asked Nov 19 '13 13:11

Developer


People also ask

What does shell_ exec() do?

The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix. If the command fails return NULL and the values are not reliable for error checking.

What does system () do in PHP?

system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.

How to use shell_ exec in PHP?

shell_exec() Function: The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix.

How to disable exec in PHP?

Under Actions, click on the Manage php.ini file: Just after 'disable_functions = ', write out the functions you want to disable (example: exec,passthru,popen). Here is a list of functions that are commonly disabled as a means to improve security: exec.


2 Answers

  • exec only returns the last line of the generated output.
  • shell_exec returns the full output of the command, when the command finished running.
  • system immediately shows all output, and is used to show text.
  • passthru also returns output immediately, but is used for binary data. passthru displays raw data.

With both exec and shell_exec it is possible to handle the output yourself, while system and passthru won't let you customize it and immediately display the output.

A more detailed comparison can be found here.

like image 52
Patrick Kostjens Avatar answered Sep 21 '22 13:09

Patrick Kostjens


passthru is used for returning binary data instead of ascii. A typical example is where an image manipulation program is returning an image instead of text data.

See PHP - exec() vs system() vs passthru() for more info

Also see php shell_exec() vs exec().

like image 32
vogomatix Avatar answered Sep 20 '22 13:09

vogomatix