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
流中输出.您可以通过将 2>&1
附加到命令,通过 shell_exec()
的正常返回字符串访问错误数据,该命令将重定向 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.
相关文章