Using PHP to execute cmd commands

拟墨画扇 提交于 2019-12-17 19:36:11

问题


How do I properly execute commands in the command line using php? For example I'm using the command below in the command line to convert a docx file into a pdf file:

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

Now using PHP code I want to be able to execute the same command but nothing seems to be happening:

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

Is this possible in PHP?If yes, how do I do it?


回答1:


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

try this.




回答2:


Don't forget to escape your command with escapeshellcmd(). This will prevent you from having to use ugly backslashes and escape characters.

There are also other alternatives which may work:

`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.

Also, make sure your .exe is included within your $PATH variable. If not, include the full path for the command.



来源:https://stackoverflow.com/questions/11209509/using-php-to-execute-cmd-commands

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!