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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建