passthru() - 执行外部程序并且显示原始输出 - php 执行命令函数
passthru()
(PHP 4, PHP 5, PHP 7)
执行外部程序并且显示原始输出
说明
passthru(string $command[,int &$return_var]): void同exec()函数类似,passthru()函数也是用来执行外部命令($command)的。当所执行的 Unix 命令输出二进制数据,并且需要直接传送到浏览器的时候,需要用此函数来替代exec()或system()函数。常用来执行诸如 pbmplus 之类的可以直接输出图像流的命令。通过设置 Content-type 为image/gif,然后调用 pbmplus 程序输出 gif 文件,就可以从 PHP 脚本中直接输出图像到浏览器。
参数
$command要执行的命令。
$return_var如果提供$return_var参数, Unix 命令的返回状态会被记录到此参数。
返回值
没有返回值。
注释
Warning当用户提供的数据传入此函数,使用escapeshellarg()或escapeshellcmd()来确保用户欺骗系统从而执行任意命令。
Note:如何程序使用此函数启动,为了能保持在后台运行,此程序必须将输出重定向到文件或其它输出流。否则会导致PHP 挂起,直至程序执行结束。
Note:安全模式启用时,可仅可用safe_mode_exec_dir执行文件。实际上,现在不允许在到可执行的路径中存在..组件。
Warning安全模式启用时,命令字符串会被escapeshellcmd()转换。因此,echo y | echo x会变成echo y | echo x。
参见
exec()
执行一个外部程序system()
执行外部程序,并且显示输出popen()
打开进程文件指针escapeshellcmd()
shell 元字符转义- 执行运算符
Regarding swbrown's comment...you need to use an output buffer if you don't want the data displayed. For example: ob_start(); passthru("command"); $var = ob_get_contents(); ob_end_clean(); //Use this instead of ob_flush() This gets all the output from the command, and exits without sending any data to stdout.
If you are using passthru() to download files (for dynamically generated content or something outside webserver root) using similar code: header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"myfile.zip\""); header("Content-Length: 11111"); passthru("cat myfile.zip",$err); and your download goes fine, but subsequent downloads / link clicks are screwed up, with headers and binary data being all over the website, try putting exit(); after the passthrough. This will exit the script after the download is done and will not interfere with any future actions.
The documention does not mention that passthru() will only display standard output and not standard error. If you are running a script you can pipe the STDERR to STDOUT by doing exec 2>&1 Eg. the script below will actually print something with the passthru() function... #!/bin/sh exec 2>&1 ulimit -t 60 cat nosuchfile.txt
passthru() seems absolutely determined to buffer output no matter what you do, even with ob_implicit_flush(). The solution seems to be to use popen() instead.
Note to Paul Giblock: the command *is* run through the shell. You can verify this on any Linux system with You'll get the content of the PATH environment variable, not the string $PATH.
Remember to use the full path (IE '/usr/local/bin/foo' instead of 'foo') when using passthru, otherwise you'll get an exit code of 127 (command not found).
Zak Estrada 14-Dec-2004 11:21 Remember to use the full path (IE '/usr/local/bin/foo' instead of 'foo') when using passthru, otherwise you'll get an exit code of 127 (command not found). Remember, you'll also get this error if your file does not have executable permission.
I wrote function, that gets proxy server value from the Internet Explorer (from registry). It was tested in Windows XP Pro (Sorry for my English)
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!