使用PHP exec命令执行批处理文件时出现问题 [英] Problem using PHP exec command to execute a batch file

查看:239
本文介绍了使用PHP exec命令执行批处理文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让PHP调用一个批处理文件,该文件将使用RTF文件,并使用OpenOffice宏将其转换为PDF.我已经在命令行上测试了该批处理文件,并且可以正常工作,但是我没有运气,也没有使用PHP中的同一批处理文件.

I'm attempting to get PHP to call a batch file which will take an RTF file and convert it to a PDF using an OpenOffice macro. I've tested the batch file on the command line and it works fine, but I'm not having any luck calling and using the same batch file from PHP.

我的机器操作系统是XP Professional SP3.我正在运行IIS 6和PHP版本5.2.9. 我已向c:\ windows \ system32 \ cmd.exe上的Internet用户授予了执行权限. 我指定了要执行的批处理文件的完整路径,以及要转换的RTF文件的完整路径.

My machine OS is XP professional SP 3. I'm running IIS 6 and PHP version 5.2.9. I've granted execute permissions to the internet user on c:\windows\system32\cmd.exe. I specified the full path to the batch file being executed and the full path to the RTF file to be converted.

PHP看起来像这样,其中$ arg是要转换的RTF:

The PHP looks like this where $arg is the RTF to be converted:

$arg = "C:\\web_root\\whatever\\tempOutput.rtf";
$command = "c:\\windows\\system32\\cmd.exe /c c:\\web_root\\whatever\\convert.bat $arg";

然后在try-catch中调用exec命令:

Then inside a try-catch I call the exec command:

exec("$command 2>&1 && exit", $ret, $err);

在捕获后我回显结果:

echo "ret: ";
print_r ($ret);
print "<br>";
echo "err is ";
echo $err;
print "<br>";
echo "DONE!";

这就是我所看到的:

ret: Array ( ) 
err is 0
DONE!

RTF文件未转换,并且没有看到错误.关于下一步可以尝试的任何想法?谢谢!!!

The RTF file does not get converted and I'm not seeing the errors. Any ideas on what I can try next? Thanks!!!

推荐答案

您是否将IIS用作Web服务器?如果是这样,则默认情况下,PHP exec函数将无法运行,并且您不应绕过阻止其运行的安全措施.

Are you using IIS as your webserver? If so, the PHP exec function will not work by default and you should NOT circumvent the security measures that prevent it from running.

检查事件查看器,您应该找到一些与您的问题有关的错误.通过google运行以下查询:IIS PHP exec.这应该为您提供有关该问题的大量信息.

Check your event viewer and you should find some errors pertaining to your problem. Run a query through google for: IIS PHP exec. This should give you a large selection of information about the problem.

基本上,PHP exec函数尝试派生新的cmd.exe实例. IIS禁止这样做,因为它可能会在系统中打开安全漏洞.

Basically, the PHP exec function tries to fork a new cmd.exe instance. IIS prohibits this because it could open a security hole in the system.

我想出的最好的解决方案是让您的php脚本将要执行的命令写入平面文件或进行数据库输入.然后,您将需要编写一个单独的脚本,该脚本由Windows调度程序启动,每10分钟运行一次,以便检查您的平面文件或数据库以查找要运行的命令.然后,新脚本将运行命令,然后放置结果或执行确认,以便您的Web应用程序稍后可以访问.

The best solution that I have come up with is to have your php script either write the command that you want to execute to a flat file or make a database entry. You will then need to write a seperate script that is launched by the windows scheduler to run every 10 minutes or so that will check your flat file or database for commands to run. The new script will then run the commands and then place either the results or an execution confirmation that your web app will be able to access at a later time.

这肯定是个矛盾.

这篇关于使用PHP exec命令执行批处理文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆