简体   繁体   中英

php exec and shell_exec not working

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()".

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.

exec()
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.

this should work:

$output = array();
exec('myprogram < INP.DAT', $output);
var_dump($output);

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

有时这些函数被禁用而php没有安全模式,你必须在php.ini中启用它们

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