4

shell_exec and exec are not returning any content. I can't figure out what's wrong.

Here's some code:

echo 'test: ';
$output = shell_exec('whoami');
var_export($output, TRUE);
echo PHP_EOL . '<br>' . PHP_EOL;

And here's the source of the output

test 2: 
<br>

I do not have control over the host, but I believe they're running SuPHP. According to phpinfo, safe_mode is off. Running whoami from SSH outputs the expected value.

I'm at a loss. Any idea how to debug this?

sharoz
  • 6,157
  • 7
  • 31
  • 57

2 Answers2

5

You're never printing the $output variable. The var_export() call returns the content of the variable when you call it with a true second parameter, it does not print it directly.

lanzz
  • 42,060
  • 10
  • 89
  • 98
0

If you want the output from a shell command read back into PHP, you're probably going to need popen(). For example:

if( ($fp = popen("some shell command", "r")) ) {
    while( !feof($fp) ) {
        echo fread($fp, 1024);
        flush(); // input will be buffered
    }
    fclose($fp);
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190