0

我试图让 PHP 调用一个批处理文件,该文件将获取一个 RTF 文件并使用 OpenOffice 宏将其转换为 PDF。我已经在命令行上测试了批处理文件,它工作正常,但我没有任何运气从 PHP 调用和使用相同的批处理文件。

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

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

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

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

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 文件没有得到转换,我没有看到错误。关于下一步我可以尝试什么的任何想法?谢谢!!!

4

4 回答 4

1

I'm going to bet this is about permissions.

In a typical setup, PHP runs as apache - so you'll want to make sure apache has the rights to execute the batch file.

also, check this relevant SO question, and this google search.

于 2009-06-02T17:27:36.680 回答
1

Looks like the output array is empty. Is your batch script supposed to have output?

Also, escapeshellcmd and escapeshellarg should be used

于 2009-06-02T17:29:42.607 回答
1

您是否使用 IIS 作为您的网络服务器?如果是这样,默认情况下 PHP exec 函数将不起作用,您不应规避阻止其运行的安全措施。

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

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

我想出的最佳解决方案是让您的 php 脚本将要执行的命令写入平面文件或创建数据库条目。然后,您将需要编写一个由 Windows 调度程序启动的单独脚本,每隔 10 分钟左右运行一次,以检查您的平面文件或数据库是否有要运行的命令。然后,新脚本将运行命令,然后放置结果或执行确认,以便您的 Web 应用程序稍后能够访问。

这肯定是一团糟。

于 2009-06-02T17:37:02.470 回答
0

Is PHP running in safe-mode? If so, shell commands are escaped with escapeshellcmd. Perhaps this is the problem?

Do you have control of the server running the PHP script?

于 2009-06-02T17:27:07.273 回答