Shell_exec in PHP returns an empty string

shell_exec and exec do not return any content. I can’t understand what happened.

Here is the code:

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

And here is the source of the output

 test 2: <br> 

I have no control over the host, but I believe that they run SuPHP. According to phpinfo , safe_mode is off. Running whoami from SSH prints the expected value.

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

+4
source share
2 answers

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

+5
source

If you want the shell command output returned in PHP, you probably need popen() . For instance:

 if( ($fp = popen("some shell command", "r")) ) { while( !feof($fp) ) { echo fread($fp, 1024); flush(); // input will be buffered } fclose($fp); } 
0
source

All Articles