PHP Exec() VS System() VS Passthru()

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

  • 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.

What are the differences of system(), exec() and shell_exec() in PHP?

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:

  • http://php.net/manual/en/function.exec.php
  • http://php.net/manual/en/function.system.php
  • http://php.net/shell_exec

PHP's passthru() vs shell_exec()

It returns a string with the data written to the standard output. Strings in PHP are binary safe; they can contain \0 characters.

Such characters will be included in the result of shell_exec() if the executed command resulted in those characters being written. What you do with that is up to you.

Output buffering is irrelevant here; shell_exec() doesn't output anything to the client, it only returns a string.

Exception Bubbling with passthru(), system(), or exec()

Like @minitech said, you would have to listen on STDERR.

When you call a PHP script via the system() access commands, it runs them as a separate process, which your calling script downs not have access to.

If you want to do it this way, you might be able to look into the POSIX extension.

If not, you will have to test the response from the system() call, rather than trying to catch the Exception

PHP exec, system or passthru all remove single or double quotes

exec() and system() commands in PHP are passed over the shell. The shell parses the commandline and removes the quotes. It however keeps the whole string as one parameter, and shovels it to the actual execve() system call etc.

The application will receive your string that was in single quotes as one parameter. The ps tool however might just list them as is.

It might however be that the lftp tool does some parsing of its own (the -e flag sounds like that). In which case two levels of quotes might help:

exec("lftp -e '\'multiple; commands; for the lftp thingy\''");

PHP misses system variables when using exec

After re-installing laragon it worked. I Have checked before uninstalling path variables and i haven't seen mine that should be in.

I couldn't modify it either so I just reinstalled it.



Related Topics



Leave a reply



Submit