如何正确在命令行中使用PHP执行命令? 比如我使用下面的命令行到的docx文件转换成PDF文件的命令:
pdfcreator.exe /PF"D:\Documents\sample.docx
现在,使用PHP代码中,我希望能够执行相同的命令,但似乎没有要发生的事情:
<?php
shell_exec('pdfcreator.exe /PF"D:\Documents\sample.docx"');
?>
这是可能的PHP?如果是的话,我该怎么办呢?
如何正确在命令行中使用PHP执行命令? 比如我使用下面的命令行到的docx文件转换成PDF文件的命令:
pdfcreator.exe /PF"D:\Documents\sample.docx
现在,使用PHP代码中,我希望能够执行相同的命令,但似乎没有要发生的事情:
<?php
shell_exec('pdfcreator.exe /PF"D:\Documents\sample.docx"');
?>
这是可能的PHP?如果是的话,我该怎么办呢?
system("c:\\path\\to\\pdfcreator.exe /PF\"D:\\Documents\\sample.docx"");
试试这个。
不要忘了逃避你的命令escapeshellcmd()将 。 这将阻止您不必使用难看反斜杠和转义字符。
也有其他的替代品,其可能工作:
`command` // back ticks drop you out of PHP mode into shell
exec('command', $output); // exec will allow you to capture the return of a command as reference
shell_exec('command'); // will return the output to a variable
system(); //as seen above.
此外,请确保您的.exe文件包含您的$ PATH变量中。 如果没有,包括命令的完整路径。