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

如何在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?

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度