PHP exec() vs system() vs passthru()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/732832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP exec() vs system() vs passthru()
提问by codingbear
What are the differences?
有什么区别?
Is there a specific situation or reason for each function? If yes, can you give some examples of those situations?
每个功能是否有特定的情况或原因?如果是,你能举出一些这些情况的例子吗?
PHP.net says that they are used to execute external programs. see referenceFrom the examples I see, I don't see any obvious difference.
PHP.net 说它们用于执行外部程序。请参阅参考从我看到的示例中,我没有看到任何明显的区别。
If I were to simply run a script (bash or python), which function do you recommend me to use?
如果我只是运行一个脚本(bash 或 python),你推荐我使用哪个函数?
回答by Kalium
They have slightly different purposes.
它们的目的略有不同。
exec()is for calling a system command, and perhaps dealing with the output yourself.system()is for executing a system command and immediately displaying the output - presumably text.passthru()is for executing a system command which you wish the raw return from - presumably something binary.
exec()用于调用系统命令,并且可能自己处理输出。system()用于执行系统命令并立即显示输出 - 大概是文本。passthru()用于执行您希望原始返回的系统命令 - 大概是二进制的东西。
Regardless, I suggest you not use any of them. They all produce highly unportable code.
无论如何,我建议您不要使用它们中的任何一个。它们都产生高度不可移植的代码。
回答by Dinesh Saini
As drawn from http://php.net/&& Chipmunkninja:
从http://php.net/&& Chipmunkninja 中提取 :
The system()Function
The system function in PHP takes a string argument with the command to execute as well as any arguments you wish passed to that command. This function executes the specified command, and dumps any resulting text to the output stream (either the HTTP output in a web server situation, or the console if you are running PHP as a command line tool). The return of this function is the last line of output from the program, if it emits text output.
The exec()Function
The system function is quite useful and powerful, but one of the biggest problems with it is that all resulting text from the program goes directly to the output stream. There will be situations where you might like to format the resulting text and display it in some different way, or not display it at all.
For this, the exec function in PHP is perfectly adapted. Instead of automatically dumping all text generated by the program being executed to the output stream, it gives you the opportunity to put this text in an array returned in the second parameter to the function:
The shell_exec()Function
Most of the programs we have been executing thus far have been, more or less, real programs1. However, the environment in which Windows and Unix users operate is actually much richer than this. Windows users have the option of using the Windows Command Prompt program, cmd.exe This program is known as a command shell.
The passthru()Function
One fascinating function that PHP provides similar to those we have seen so far is the passthru function. This function, like the others, executes the program you tell it to. However, it then proceeds to immediately send the raw output from this program to the output stream with which PHP is currently working (i.e. either HTTP in a web server scenario, or the shell in a command line version of PHP).
The proc_open()Function and popen()function
proc_open() is similar to popen() but provides a much greater degree of control over the program execution. cmd is the command to be executed by the shell. descriptorspec is an indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process. pipes will be set to an indexed array of file pointers that correspond to PHP's end of any pipes that are created. The return value is a resource representing the process; you should free it using proc_close() when you are finished with it.
该系统()函数
PHP 中的系统函数接受一个字符串参数和要执行的命令以及您希望传递给该命令的任何参数。此函数执行指定的命令,并将任何结果文本转储到输出流(Web 服务器情况下的 HTTP 输出,或者如果您将 PHP 作为命令行工具运行时的控制台)。如果它发出文本输出,则此函数的返回是程序输出的最后一行。
在EXEC()函数
system 函数非常有用和强大,但它最大的问题之一是程序产生的所有文本都直接进入输出流。在某些情况下,您可能希望格式化结果文本并以不同的方式显示它,或者根本不显示它。
为此,PHP 中的 exec 函数进行了完美的调整。它不是自动将正在执行的程序生成的所有文本转储到输出流,而是让您有机会将此文本放入函数的第二个参数中返回的数组中:
到目前为止,我们执行的大多数程序或多或少都是真正的程序1。但是,Windows 和 Unix 用户操作的环境实际上比这丰富得多。Windows 用户可以选择使用 Windows 命令提示符程序 cmd.exe 该程序称为命令外壳程序。
的通路()函数
PHP 提供的一个与我们目前所见类似的迷人函数是 passthru 函数。这个函数和其他函数一样,执行你告诉它的程序。但是,它随后立即将来自该程序的原始输出发送到 PHP 当前正在使用的输出流(即 Web 服务器场景中的 HTTP,或 PHP 命令行版本中的 shell)。
的proc_open()函数和POPEN()函数
proc_open() 类似于 popen() 但提供了对程序执行的更大程度的控制。cmd是shell要执行的命令。descriptorspec 是一个索引数组,其中键表示描述符编号,值表示 PHP 将如何将该描述符传递给子进程。管道将被设置为文件指针的索引数组,该数组与创建的任何管道的 PHP 结尾相对应。返回值是代表进程的资源;完成后,您应该使用 proc_close() 释放它。
回答by orrd
The previous answers seem all to be a little confusing or incomplete, so here is a table of the differences...
以前的答案似乎都有点令人困惑或不完整,所以这里有一个差异表......
+----------------+-----------------+----------------+----------------+
| Command | Displays Output | Can Get Output | Gets Exit Code |
+----------------+-----------------+----------------+----------------+
| system() | Yes (as text) | Last line only | Yes |
| passthru() | Yes (raw) | No | Yes |
| exec() | No | Yes (array) | Yes |
| shell_exec() | No | Yes (string) | No |
| backticks (``) | No | Yes (string) | No |
+----------------+-----------------+----------------+----------------+
- "Displays Output" means it streams the output to the browser (or command line output if running from a command line).
- "Can Get Output" means you can get the output of the command and assign it to a PHP variable.
- The "exit code" is a special value returned by the command (also called the "return status"). Zero usually means it was successful, other values are usually error codes.
- “显示输出”意味着它将输出流式传输到浏览器(如果从命令行运行,则为命令行输出)。
- “可以获取输出”意味着您可以获取命令的输出并将其分配给 PHP 变量。
- “退出代码”是命令返回的特殊值(也称为“返回状态”)。零通常表示成功,其他值通常是错误代码。
Other misc things to be aware of:
其他需要注意的其他事项:
- The shell_exec() and the backticks operator do the same thing.
- There are also proc_open() and popen() which allow you to interactively read/write streams with an executing command.
- Add "2>&1" to the command string if you also want to capture/display error messages.
- Use escapeshellcmd() to escape command arguments that may contain problem characters.
- If passing an $output variable to exec() to store the output, if $output isn't empty, it will append the new output to it. So you may need to unset($output) first.
- shell_exec() 和反引号操作符做同样的事情。
- 还有 proc_open() 和 popen() 允许您使用正在执行的命令交互地读/写流。
- 如果您还想捕获/显示错误消息,请将“2>&1”添加到命令字符串中。
- 使用 escapeshellcmd() 转义可能包含问题字符的命令参数。
- 如果将 $output 变量传递给 exec() 以存储输出,如果 $output 不为空,它会将新输出附加到它。所以你可能需要先 unset($output) 。
回答by Cody Caughlan
It really all comes down to how you want to handle output that the command might return and whether you want your PHP script to wait for the callee program to finish or not.
这实际上完全取决于您希望如何处理命令可能返回的输出,以及您是否希望 PHP 脚本等待被调用程序完成。
execexecutes a command and passes output to the caller (or returns it in an optional variable).passthruis similar to theexec()function in that it executes a command . This function should be used in place ofexec()orsystem()when the output from the Unix command is binary data which needs to be passed directly back to the browser.systemexecutes an external program and displays the output, but only the last line.
exec执行命令并将输出传递给调用者(或在可选变量中返回它)。passthru与exec()执行命令的函数类似。这个功能应该代替使用exec()或system()从Unix的命令的输出时是需要被直接返回到浏览器传递的二进制数据。system执行一个外部程序并显示输出,但只显示最后一行。
If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru()function.
如果您需要执行一个命令并且让命令中的所有数据直接传回而不受任何干扰,请使用该passthru()函数。
回答by Matt
If you're running your PHP script from the command-line, passthru()has one large benefit. It will let you execute scripts/programs such as vim, dialog, etc, letting those programs handle control and returning to your script only when they are done.
如果您从命令行运行 PHP 脚本,则passthru()有一大好处。它可以让你执行脚本/程序,如vim,dialog等,让这些程序处理控制并返回到你的脚本,只有当他们这样做。
If you use system()or exec()to execute those scripts/programs, it simply won't work.
如果您使用system()或exec()执行这些脚本/程序,它根本无法工作。
Gotcha: For some reason, you can't execute lesswith passthru()in PHP.
问题:出于某种原因,您无法在 PHP 中执行lesswith passthru()。

