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:
code.java is created successfully and filled with the data I specify.
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?
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.
As mentioned, output to STDERR
goes to Apache's log files. To "see" it in the browser, you'll need to redirect it to STDOUT
:
$command = "javac -verbose code.java 2>&1";
$result = passthru($command);
This is called file descriptor redirection.