使用PHP执行CMD命令(Using PHP to execute cmd commands)

2019-06-25 18:35发布

如何正确在命令行中使用PHP执行命令? 比如我使用下面的命令行到的docx文件转换成PDF文件的命令:

pdfcreator.exe /PF"D:\Documents\sample.docx

现在,使用PHP代码中,我希望能够执行相同的命令,但似乎没有要发生的事情:

<?php
shell_exec('pdfcreator.exe /PF"D:\Documents\sample.docx"');
?>

这是可能的PHP?如果是的话,我该怎么办呢?

Answer 1:

system("c:\\path\\to\\pdfcreator.exe /PF\"D:\\Documents\\sample.docx""); 

试试这个。



Answer 2:

不要忘了逃避你的命令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变量中。 如果没有,包括命令的完整路径。



文章来源: Using PHP to execute cmd commands