PHP - 如何获取Shell错误回显到屏幕 [英] PHP - How to get Shell errors echoed out to screen

查看:200
本文介绍了PHP - 如何获取Shell错误回显到屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在第一次使用 shell_exec()的过程。我正在尝试使用ffmpeg shell脚本转换我的服务器上的一些视频文件。

I am in the process of using shell_exec() for the first time. I am trying to convert some video files on my server using the ffmpeg shell script.

当浏览器中的以下代码返回NULL时:

When I the below code in the browser, it returns NULL:

var_dump(shell_exec("ffmpeg -i /var/www/html/sitedomain/httpdocs/tmp/ebev1177.mp4"));

但是,当我在终端上运行等效代码时:

However when I run the equivalent code in my terminal:

> ffmpeg -i /var/www/html/sitedomain/httpdocs/tmp/ebev1177.mp4

我收回了一大堆有用的信息其结尾为错误必须至少指定一个输出文件

I get back a whole load of useful information which ends in an error "At least one output file must be specified"

为什么这个信息不被传递回到我的PHP脚本,所以我可以回应它?

Why is this info not being passed back to my PHP script so I can echo it out?

推荐答案

错误数据是从目标程序的 STDERR 流。您可以从 shell_exec()通过追加 2>& 1 通过正常返回的字符串访问错误数据到命令,将重定向 STDERR STDOUT ,您当前看到的流:

The error data is output from the target program's STDERR stream. You can get access to the error data through the normal returned string from shell_exec() by appending 2>&1 to the command, which will redirect STDERR to STDOUT, the stream that you are currently seeing:

var_dump(shell_exec("ffmpeg -i /var/www/html/sitedomain/httpdocs/tmp/ebev1177.mp4 2>&1"));

您可能还想看看 proc_open() ,这将允许您访问 STDIN STDOUT STDERR 作为三个独立的流,它可以对目标程序进行更精细的粒度控制,以及如何处理输入和输出,包括如果需要,将其中的任何一个和全部直接重定向到日志文件。请注意,这是一个更复杂的机制,具有许多陷阱和绊倒危险。

You may also want to take a look at proc_open() which will allow you to get access to STDIN, STDOUT and STDERR as three individual streams, which can afford much finer grained control over the target program and exactly how you handle the input and output to it, including redirecting any and all of them directly to a log file if so desired. Be aware though that this is a much more complex mechanism with many pitfalls and tripping hazards.

有关标准流的更多信息,请参见这里

More information on the standard streams can be found here.

这篇关于PHP - 如何获取Shell错误回显到屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆