Using PHP to execute cmd commands

How to execute commands on the command line with php? For example, I use the command below on the command line to convert a docx file to a pdf file:

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

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

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

Is this possible in PHP? If so, how to do it?

+5
source share
2 answers
system("c:\\path\\to\\pdfcreator.exe /PF\"D:\\Documents\\sample.docx""); 

try it.

+8
source

Remember to avoid the escapeshellcmd () command . This will not allow you to use ugly backslashes and escape characters.

There are other alternatives that 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.

, , .exe $PATH. , .

+4

All Articles