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?

4
  • ### Update Do you get any errors in your logs? What happens if you wrap that shell_exec call in a var_export? ### Original Have you tried system() instead? Here is the documentation: php.net/manual/en/function.system.php. Commented Jun 26, 2012 at 14:25
  • I've tried system and all the other functions for executing commands on the system(exec, shell_exec, system, pcntl_exec, passthru) Commented Jun 26, 2012 at 14:27
  • I'm not the one who downvoted, your answer is much appreciated. Commented Jun 26, 2012 at 14:28
  • No problem man. I probably deserved the -1, but it is good form to explain why a downvote occurred. I didn't think you were the one who downvoted anyway. On topic: I would check out sixeightzero's answer. Using escapeshellcmd() would accomplish Piotr Olaszewski's answer but without the manual escaping. Good luck! Commented Jun 26, 2012 at 14:34

2 Answers 2

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

try this.

Sign up to request clarification or add additional context in comments.

3 Comments

do I still need to include the path to the executable file if I already included it in the environment variables?
I'm not for sure, but safety is pass all path.
I get a syntax error so I changed it to something like this based on the code that you provided: system('C:\\Program Files\\PDFCreator\\pdfcreator.exe /PF\"D:\\Documents\\sample.docx"'); but still no luck.
6

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.

2 Comments

Tried the code below based on your answer, but still doesn't work. Did I mess something up: <?php $command = escapeshellcmd('pdfcreator.exe /PF"D:\Documents\sample.docx"'); $command; exec($command, $output); shell_exec($command); system(); ?>
When you run the script from CLI does it output anything? Try something like: $command = escapeshellcmd('pdfcreator.exe /PF"D:\Documents\sample.docx" > C:\outfile');. does anything get written to C:\outfile?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.