PHP exec() vs system() vs passthru()
有什么区别?
每个功能是否有特定情况或原因? 如果是,您能举一些例子吗?
PHP.net说,它们用于执行外部程序。 见参考
从我看到的示例中,我看不出任何明显的区别。
如果我只是运行脚本(bash或python),那么建议我使用哪个功能?
它们的用途略有不同。
-
exec() 用于调用系统命令,并可能自己处理输出。 -
system() 用于执行系统命令并立即显示输出-可能是文本。 -
passthru() 用于执行您希望从中原始返回的系统命令-大概是二进制文件。
无论如何,我建议您不要使用其中任何一个。它们都产生高度不可移植的代码。
摘自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()
functionproc_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.
先前的答案似乎有些混乱或不完整,因此下面是差异表...
1 2 3 4 5 6 7 8 9 | +----------------+-----------------+----------------+----------------+ | 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 | +----------------+-----------------+----------------+----------------+ |
- "显示输出"表示它将输出流式传输到浏览器(如果从命令行运行,则将其输出到命令行)。
- "可以获取输出"意味着您可以获取命令的输出并将其分配给PHP变量。
- "退出代码"是命令返回的特殊值(也称为"返回状态")。零通常表示成功,其他值通常是错误代码。
其他需要注意的杂项:
- shell_exec()和反引号运算符执行相同的操作。
- 还有proc_open()和popen(),它们允许您使用执行命令以交互方式读取/写入流。
- 如果您还想捕获/显示错误消息,请在命令字符串中添加" 2>&1"。
- 使用escapeshellcmd()可以转义可能包含问题字符的命令参数。
- 如果将$ output变量传递给exec()来存储输出,如果$ output不为空,它将在其后追加新的输出。因此,您可能需要先取消设置($ output)。
实际上,这全都取决于您要如何处理命令可能返回的输出以及是否希望PHP脚本等待被调用程序完成。
-
exec 执行命令并将输出传递给调用方(或将其返回到可选变量中)。 -
passthru 与exec() 函数的相似之处在于它执行命令。当Unix命令的输出是二进制数据且需要直接传递回浏览器时,应使用此函数代替exec() 或system() 。 -
system 执行一个外部程序并显示输出,但仅显示最后一行。
如果您需要执行命令并直接将命令中的所有数据传回而不会受到任何干扰,请使用
如果您从命令行运行PHP脚本,则
如果使用
陷阱:由于某种原因,您无法在PHP中使用