問題描述
我正在第一次使用 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.
這篇關于PHP - 如何讓 Shell 錯誤回顯到屏幕上的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!