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()”。
4 个解决方案
#1
One of the comments on the shell_exec
manual 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 returnNULL
instead.请注意以下不一致:如果命令的输出为空,则shell_exec()和反引号操作符不会返回字符串 - 它们将返回NULL。
This will make strict comparisons to
''
returnfalse
.这将严格比较''返回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.shell_exec()(反引号的功能等价物)当PHP以安全模式运行时,此功能被禁用。
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.exec()您只能在safe_mode_exec_dir中执行可执行文件。出于实际原因,目前不允许在可执行文件的路径中包含..组件。 escapeshellcmd()在此函数的参数上执行。
You can check your server's PHP settings with the phpinfo()
function.
您可以使用phpinfo()函数检查服务器的PHP设置。
#2
this should work:
这应该工作:
$output = array(); exec('myprogram < INP.DAT', $output); var_dump($output);
#3
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
myprogram是否可以从默认shell获得?它在特定目录中吗?尝试用/ full / path / to / myprogram
#4
Sometimes these functions are disabled without the php are in safemode, you have to enable them in php.ini
有时这些函数被禁用而php没有安全模式,你必须在php.ini中启用它们
#1
One of the comments on the shell_exec
manual 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 returnNULL
instead.请注意以下不一致:如果命令的输出为空,则shell_exec()和反引号操作符不会返回字符串 - 它们将返回NULL。
This will make strict comparisons to
''
returnfalse
.这将严格比较''返回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.shell_exec()(反引号的功能等价物)当PHP以安全模式运行时,此功能被禁用。
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.exec()您只能在safe_mode_exec_dir中执行可执行文件。出于实际原因,目前不允许在可执行文件的路径中包含..组件。 escapeshellcmd()在此函数的参数上执行。
You can check your server's PHP settings with the phpinfo()
function.
您可以使用phpinfo()函数检查服务器的PHP设置。
#2
this should work:
这应该工作:
$output = array(); exec('myprogram < INP.DAT', $output); var_dump($output);
#3
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
myprogram是否可以从默认shell获得?它在特定目录中吗?尝试用/ full / path / to / myprogram
#4
Sometimes these functions are disabled without the php are in safemode, you have to enable them in php.ini
有时这些函数被禁用而php没有安全模式,你必须在php.ini中启用它们