使用 PHP 的 exec() 函数(或类似函数),如何显示终端错误输出?

发布于 2024-12-17 17:27:45 字数 701 浏览 1 评论 0原文

可能的重复:
我怎样才能知道命令中的错误在 exec 函数中?

我只是想知道是否有一种方法(使用 PHP)将编译错误信息从终端输出到浏览器?

例如: 如果您使用此代码在终端中执行...

gcc -o try try.c

...并假设“try.c”有一些错误。 终端将输出如下内容:

  • try.c In function 'main':
  • try.c:27 'x' undeclared (first use in this function)
  • try.c:27 (每个未声明的标识符仅报告一次
  • try.c: 27 对于它出现的每个函数。)

同时,使用这个 PHP 代码...

浏览器不会像终端那样返回任何输出。是否有可能直接或间接地将终端显示的错误详细信息传递到浏览器上显示?

非常感谢。

Possible Duplicate:
How can I know the error in command in an exec function?

I am just wondering if there's a way (using PHP) to output the compile error information from the terminal to the browser?

For example:
If you use this code to execute in the terminal...

gcc -o try try.c

...and assuming the "try.c" have some errors.
The terminal will output something like this:

  • try.c In function 'main':
  • try.c:27 'x' undeclared (first use in this function)
  • try.c:27 (Each undeclared identifier is reported only once
  • try.c:27 for each function it appears in.)

Meanwhile, using this PHP code...

<?php
exec("gcc -o try try.c");
?>

The browser does not return any output like the terminal does. Is there a possible way direct or indirect that the error details that the terminal showed can be passed to the be displayed on a browser?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-12-24 17:27:45

正如 @hakre 在评论中指出的那样 - 您可以简单地重定向 STDERR 流,但这里有另一个解决方案:

exec('command here', $output, $exit_status);
if ($exit_status !== 0) echo implode('<br />', $output);

如果执行成功 - 每个程序都应返回 0 的退出状态,任何其他代码都应意味着某种错误。

As @hakre has noted in the comments - you can simply redirect the STDERR stream, but here's another solution:

exec('command here', $output, $exit_status);
if ($exit_status !== 0) echo implode('<br />', $output);

If execution was successful - each program should return an exit status of 0 and any other code should mean some kind of an error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文