PHP - How to get Shell errors echoed out to screen

非 Y 不嫁゛ 提交于 2019-11-27 17:16:57

问题


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.

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"

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


回答1:


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"));

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.



来源:https://stackoverflow.com/questions/15086572/php-how-to-get-shell-errors-echoed-out-to-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!