dongyan3616
2011-08-31 06:53
浏览 61
已采纳

如何在PHP中使用passthru显示javac错误消息?

As I understand it, PHP's passthru displays the unadulterated output of the console - including all outputs, like STDERR. I'm trying to javac a file from a PHP file like so:

<?php
error_reporting(E_ALL);

    if(file_put_contents("code.java", "aaaaaa"))
    {
        passthru("javac -verbose code.java");
        echo("Done.");
    }
    else
    {
        echo("UNEXPECTED PHP ERROR");
    }
?> 

As you can probably guess, "aaaaaaa" should NOT compile - and this I expect output from the javac call (not to mention the -verbose). However, when I access the web page, I notice two things:

  1. code.java is created successfully and filled with the data I specify.

  2. The only output to the webpage is "Done."

Note that if I call the exact same command from the cmd prompt, I get a whole slew of output. What's going on here?

tl;dr; why am I not getting any output from passthru()?

EDIT: If I change the passthru command string to "echo PLEASEWORK" it displays outputs correctly


For some reason I can't comment - Quamis, what IS the prefered method of executing this cmd and capturing all output?

图片转代码服务由CSDN问答提供 功能建议

据我所知,PHP的passthru显示控制台的纯粹输出 - 包括所有输出,如STDERR。 我正在尝试从PHP文件中获取文件,如下所示:

 &lt;?php 
error_reporting(E_ALL); 
 
 if(file_put_contents(“code。  java“,”aaaaaa“))
 {
 passthru(”javac -verbose code.java“); 
 echo(”Done。“); 
} 
 else 
 {
 echo(”UNEXPECTED“  PHP ERROR“); 
} 
?&gt;  
   
 
 

正如您可能猜到的那样,“aaaaaa”不应该编译 - 我希望javac调用的输出(更不用说-verbose)。 但是,当我访问网页时,我注意到两件事:

  1. code.java已成功创建并填充了我指定的数据。

  2. 网页的唯一输出是“完成”。

    请注意,如果我打电话 来自cmd提示符的完全相同的命令,我得到了一大堆输出。 这里发生了什么?

    tl; dr; 为什么我没有从passthru()得到任何输出?

    编辑:如果我将passthru命令字符串更改为“echo PLEASEWORK”,它会正确显示输出 < hr>

    出于某种原因,我无法发表评论 - Quamis,执行此cmd和捕获所有输出的首选方法是什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dsjxgu4759 2011-08-31 07:00
    最佳回答

    In the man file http://php.net/manual/en/function.passthru.php i can read clearly "This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser". This means passthry is not your best friend for this task:)

    This is not your case...

    try using passthru("your_command_here 2>&1") to capture both stdout and stderr.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)