Php-exec命令不适用于Windows,适用于命令行


Php exec command not working on Windows, works on command line

我正试图通过PHP的exec函数执行以下命令:

D:''pstill -F a4 -2 -c -c -c -c -g -i -t -K -d 700 -a 4 -m XimgAsCMYK -m Xspot -m Xoverprint -o D:''outputfile.pdf D:''new.jpg  

它不生成任何输出。但如果我直接将命令粘贴到命令行上,那么它就可以工作了。。。

注意:当直接在命令行上运行时,需要一些时间才能完成。

我建议在这里使用shell_exec而不是exec函数。shell_exec通过适当的shell(WIndows/Unix等)执行命令,并将完整的输出作为字符串返回给您。

如果您的命令是:

exec("pstill -F a4 -2 -c -c -c -c -g -i -t -K -d 700 -a 4 -m XimgAsCMYK -m Xspot -m Xoverprint -o D:''outputfile.pdf D:''input.jpg");

PHP转义反斜杠,因此到达shell的命令是... D:'outputfile.pdf D:'input.jpg。您必须对反斜杠进行双转义:一次用于PHP,一次用于shell。

exec("pstill -F a4 -2 -c -c -c -c -g -i -t -K -d 700 -a 4 -m XimgAsCMYK -m Xspot -m Xoverprint -o D:''''outputfile.pdf D:''''input.jpg");

对我来说,解决方案是

在终端窗口而不是服务中运行apache

请参阅此线程:apache服务php exec不工作