百科狗-知识改变命运!
--

passthru() - 执行外部程序并且显示原始输出 - php 执行命令函数

梵高1年前 (2023-11-21)阅读数 24#技术干货
文章标签命令

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:

passthru() - 执行外部程序并且显示原始输出 - php 执行命令函数

如何程序使用此函数启动,为了能保持在后台运行,此程序必须将输出重定向到文件或其它输出流。否则会导致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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)