php exec 和 shell_exec 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1107622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php exec and shell_exec not working
提问by chris
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.
我想在我的服务器上运行一个 exe 文件并将输出返回到浏览器屏幕。exe 文件接受一个输入文件,然后在屏幕上返回数据。
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()".
它在浏览器屏幕上显示“NULL”。我也试过 exec()。在那里它返回“Array()”。
回答by John Kugelman
One of the commentson the shell_execmanual page says:
shell_exec手册页上的评论之一说:
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 returnNULLinstead.This will make strict comparisons to
''returnfalse.
请注意以下不一致:
shell_exec()如果命令的输出为空,反引号运算符将不会返回字符串——NULL而是返回。这将使严格的比较
''返回false。
It may be disabled if PHP is in safe mode.
如果 PHP 处于安全模式,它可能会被禁用。
shell_exec()(functional equivalent of backticks)
This function is disabled when PHP is running in safe mode.
exec()
You can only execute executables within thesafe_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.
shell_exec()(功能等效于反引号)
当 PHP 在安全模式下运行时,此功能被禁用。
exec()
您只能在safe_mode_exec_dir. 出于实际原因,目前不允许..在可执行文件的路径中包含组件。escapeshellcmd()在此函数的参数上执行。
You can check your server's PHP settings with the phpinfo()function.
您可以使用该phpinfo()功能检查服务器的 PHP 设置。
回答by taber
this should work:
这应该有效:
$output = array();
exec('myprogram < INP.DAT', $output);
var_dump($output);
回答by r00fus
Is myprogram available from a default shell? Is it in a specific directory?
Try replacing myprogram < INP.DATwith /full/path/to/myprogram < INP.DAT
myprogram 是否可从默认 shell 使用?它在特定目录中吗?
尝试替换myprogram < INP.DAT为/full/path/to/myprogram < INP.DAT
回答by Teodoros
Sometimes these functions are disabled without the php are in safemode, you have to enable them in php.ini
有时这些功能在没有 php 处于安全模式的情况下被禁用,您必须在 php.ini 中启用它们

