10

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?

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • ### 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: http://www.php.net/manual/en/function.system.php. – Eric H Jun 26 '12 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) – Wern Ancheta Jun 26 '12 at 14:27
  • I'm not the one who downvoted, your answer is much appreciated. – Wern Ancheta Jun 26 '12 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! – Eric H Jun 26 '12 at 14:34

2 Answers2

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

try this.

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
  • do I still need to include the path to the executable file if I already included it in the environment variables? – Wern Ancheta Jun 26 '12 at 14:29
  • I'm not for sure, but safety is pass all path. – Piotr Olaszewski Jun 26 '12 at 14:30
  • 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. – Wern Ancheta Jun 26 '12 at 14:41
5

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.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
  • Tried the code below based on your answer, but still doesn't work. Did I mess something up: – Wern Ancheta Jun 26 '12 at 14:49
  • 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? – Mike Mackintosh Jun 26 '12 at 14:58