Take the 2-minute tour ×

I want to run an exe file on my server and return the output to the browser screen. The exe file takes a input file and then returns data on the screen.

Why is this code not working?

$output = shell_exec('myprogram < INP.DAT');
echo "<pre>" . var_export($output, TRUE) ."</pre>\\n";

It displays "NULL" on the browser screen. I have also tried exec(). There it returns "Array()".

share|improve this question
add comment

4 Answers

One of the comments on the shell_exec manual page says:

Beware of the following inconsistency: shell_exec() and the backtick operator will not return a string if the command's output is empty -- they'll return NULL instead.

This will make strict comparisons to '' return false.

It may be disabled if PHP is in safe mode.

shell_exec() (functional equivalent of backticks)
This function is disabled when PHP is running in safe mode.

You can only execute executables within the safe_mode_exec_dir. For practical reasons it's currently not allowed to have .. components in the path to the executable. escapeshellcmd() is executed on the argument of this function.

You can check your server's PHP settings with the phpinfo() function.

share|improve this answer
so if exec('ls') works, can it still be in safe mode? –  chris Jul 10 '09 at 4:16
it's not on safe mode –  chris Jul 10 '09 at 4:17
Updated, more promising finding –  John Kugelman Jul 10 '09 at 4:35
GOD BLESS YOU :* –  bizzr3 Dec 25 '13 at 16:04
add comment

this should work:

$output = array();
exec('myprogram < INP.DAT', $output);
share|improve this answer
thanks, unfortunately it just displays "array(0){ }" –  chris Jul 10 '09 at 4:15
thats because the array you're creating there is empty... –  ing0 Aug 8 '11 at 14:26
However, the exec function should fill the $output array with the lines returned from running the shell command so this should work –  htech Feb 24 at 21:24
add comment

Is myprogram available from a default shell? Is it in a specific directory?
Try replacing myprogram < INP.DAT with /full/path/to/myprogram < INP.DAT

share|improve this answer
add comment

Sometimes these functions are disabled without the php are in safemode, you have to enable them in php.ini

share|improve this answer
add comment

Your Answer


