PHP Shell_Exec() Get Complete Output as String

Posted on October 12, 2018 at 12:59 pm

With PHP you can use shell_exec() to execute a command via shell and return the complete output as a string, here si a quick example to do so:

$output = shell_exec('ls -lart');

However some commands like “gunzip -t” may show empty results.

To fix that, just add 2>&1 to the end of the command, i.e:

echo shell_exec("gunzip -c -t /path/to/file.gz 2>&1");

The issue was a line break at the beginning of the gunzip output.

Thanks to Trev for this smart tip!

Receive updates via email

Other Posts

Updated Posts