68

It is possible to run an external command by three PHP functions of

system();
exec();
shell_exec();

but what are their differences? In spite of their specific applications, in most cases, the can be equally used. I am curious to know which is preferred one when they can be equally used. For example, for unzipping a file or compressing a folder (with tar command), which one is preferred (probably from performance point of view)?

UPDATE: In another question, I found a very useful link describing different aspects for these functions. I share the link here, as other may use to better understand security issues and other aspects.

7
  • 5
    Not to be snippy, but what did the manual say about the respective functions?
    – GordonM
    May 31, 2012 at 6:54
  • 2
    Also, you forgot passthru () :)
    – GordonM
    May 31, 2012 at 6:55
  • @GordonM I read the manual, but I do not understand the preference for each function. For many cases, they can be equally used. I want a practical view.
    – Googlebot
    May 31, 2012 at 6:59
  • 1
    Possible duplicate of stackoverflow.com/questions/732832/…
    – periklis
    May 31, 2012 at 7:26
  • @periklis you're right, that question addresses the same issue, but I was unable to find it. One answer has a quite useful link for detailed explanations. Thanks!
    – Googlebot
    May 31, 2012 at 7:40

1 Answer 1

93

exec — Execute an external program

system — Execute an external program and display the output

shell_exec — Execute command via shell and return the complete output as a string

so if you don't need the output, I would go with exec.

Further details:

1
  • 7
    exec() returns the last line of output, so if you're calling a simple program that just outputs a single value like whoami or pwd, exec() is perfect.
    – alanaktion
    Dec 2, 2015 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.