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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I changed it to echo var_export($output, TRUE);. Now it outputs NULL. Any ideas?
@sharoz Try shell_exec('/usr/bin/whoami'); instead, and make sure /usr/bin is the correct path.
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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.