PHP - 如何让 Shell 错误回显到屏幕上

PHP - How to get Shell errors echoed out to screen(PHP - 如何让 Shell 错误回显到屏幕上)
本文介绍了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() 的正常返回字符串访问错误数据,该命令将重定向 STDERRSTDOUT,您当前看到的流:

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() 这将允许您获得将 STDINSTDOUTSTDERR 作为三个单独的流访问,这可以对目标程序进行更细粒度的控制,以及您如何处理输入和输出到它,包括将它们中的任何一个和所有直接重定向到日志文件(如果需要).但请注意,这是一个复杂得多的机制,存在许多陷阱和绊倒危险.

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 错误回显到屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)