简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM