sharoz
sharoz

Reputation: 6345

shell_exec in PHP returns empty string

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?

Upvotes: 4

Views: 2299

Answers (2)

lanzz
lanzz

Reputation: 43158

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.

Upvotes: 5

paulsm4
paulsm4

Reputation: 121599

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);
}

Upvotes: 0

Related Questions