Exec and shell_exec not working

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Exec and shell_exec not working

Postby ZumJ » 18. January 2011 04:21

Hey Everyone,

I've spent all day trying to get this to work, and I imagine it's just a simple thing I'm missing repeatedly.

The goal is to run this on my Windows XAMPP install:
Code: Select all
exec('pdftk docstorage/activedocs/documentsId_13_CERT-1001_0.pdf fill_form C:\xampp\htdocs\AllPure\fdf59DE.tmp output docstorage/staticdocs/CofCs/inbox/OrderDetailId_0.pdf flatten');

Doing so generates this error in the Apache log: "'pdftk' is not recognized as an internal or external command,
operable program or batch file."

So then I looked at the path by displaying phpinfo():
Code: Select all
C:\Program Files (x86)\CollabNet\Subversion Server;C:\Windows\system32;C:\Windows\System32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Java\jre6\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Palm\PDK\bin;C:\Program Files (x86)\Palm\SDK\bin;C:\Program Files (x86)\Palm\SDK\bin\novacom

Since pdftk.exe is in the System32 folder, it looks like this should be picking it up, though it doesn't appear to be able to see it.

So then I tried this:
Code: Select all
exec("C:\\Windows\\System32\\mspaint.exe",$output, $return);

But the browser just hangs indefinitely, and yet doesn't produce any error messages in either the PHP or Apache logs.

I'm seriously out of ideas, which is quite depressing. I'd like to also mention that the pdftk command works flawlessly in my ubuntu XAMPP install. If any of you could shed some light on things to try, or what I might be missing, I'd greatly appreciate it.

John
ZumJ
 
Posts: 12
Joined: 18. January 2011 04:03

Re: Exec and shell_exec not working

Postby Sharley » 18. January 2011 04:45

Because of the Apache error message you should be telling Apache where to find the command pdftk as Apache knows not how to search the System paths - only a suggestion based on the error message.

Also Apache will have problems with any path that contains spaces and other characters like brackets etc. especially the Program File (X86) folder name.

Apache likes Windows paths to be contained with quote marks " " which may also be an issue here but is not a requirement in Linux paths and may be part of the reason your script works in Linux but not in Windows XAMPP..

Hope this helps a little.

Good luck.
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3

Re: Exec and shell_exec not working

Postby peterwt » 18. January 2011 12:37

Your code -

Code: Select all
exec("C:\\Windows\\System32\\mspaint.exe",$output, $return);
works fine for me and opens Paint.

Try something simple such as

Code: Select all
system("DIR");
If this does not work then you need to look at your php configuration.

Peter
Peter
peterwt
 
Posts: 42
Joined: 17. March 2009 11:06
Operating System: Windows 7 64 bit Professional

Re: Exec and shell_exec not working

Postby ZumJ » 18. January 2011 13:53

system("DIR") works for me, and prints a whole bunch of stuff to the screen. That's good news, and thanks for suggesting a simpler command.

From Ms. Sharley's suggestion, I tried enclosing the command in double quotes inside of single quotes, like this (sans spaces between quotes):
Code: Select all
exec(' "pdftk C:/xampp/htdocs/AllPure/docstorage/activedocs/documentsId_13_CERT-1001_0.pdf fill_form C:\xampp\htdocs\AllPure\fdf167A.tmp output C:/xampp/htdocs/AllPure/docstorage/staticdocs/CofCs/inbox/OrderDetailId_0.pdf flatten" ');


This led to an error message of "The filename, directory name, or volume label syntax is incorrect."

This looks like considerable progress to me, as I know that my php config is working, at least to some degree, and it appears to actually be attempting to process my command now. Any suggestions on this latest error message?

Thanks!
ZumJ
 
Posts: 12
Joined: 18. January 2011 04:03

Re: Exec and shell_exec not working

Postby peterwt » 18. January 2011 18:06

Hi ZumJ,

In your last post showing -

Code: Select all
exec(' "pdftk C:/xampp/htdocs/AllPure/docstorage/activedocs/documentsId_13_CERT-1001_0.pdf fill_form C:\xampp\htdocs\AllPure\fdf167A.tmp output C:/xampp/htdocs/AllPure/docstorage/staticdocs/CofCs/inbox/OrderDetailId_0.pdf flatten" ');

You have mixed forward and back slashes and used only single slashes. To escape a slash you need a double \\, as you used in the paint and DIR examples. You have also used both single and double quotes at the same time. I think the line should be-

Code: Select all
exec("pdftk C:\\xampp\\htdocs\\AllPure\\docstorage\\activedocs\\documentsId_13_CERT-1001_0.pdf fill_form C:\\xampp\\htdocs\\AllPure\\fdf167A.tmp output C:\\xampp\\htdocs\\AllPure\\docstorage\\staticdoc\\CofCs\\inbox\\OrderDetailId_0.pdf flatten");


I can't see why -

Code: Select all
exec("C:\\Windows\\System32\\mspaint.exe",$output, $return);

does not work. Try -
Code: Select all
system("C:\\Windows\\System32\\mspaint.exe");


This should open Paint. If is does not try entering C:\Windows\System32\mspaint.exe in the windows "Search programs and files" box at the bottom of the start menu to confirm that you have the correct location of paint.

Regards

Peter
Peter
peterwt
 
Posts: 42
Joined: 17. March 2009 11:06
Operating System: Windows 7 64 bit Professional

Re: Exec and shell_exec not working

Postby ZumJ » 19. January 2011 21:52

Hmm, this is odd.

None of these work (stopping and restarting the apache server in between):
Code: Select all
exec('C:\\Windows\\System32\\mspaint.exe');
system("C:\\Windows\\System32\\mspaint.exe");
system("mspaint");
system("mspaint.exe");

All it does is sit there with the hourglass of doom; no error messages, no time limit exceeded, nothing.

So I was reading the notes on the PHP documentation, and it sounds like it could possibly be a permissions issue(?) So I made a new directory, gave Everyone full control, and copied cmd.exe and mspaint.exe to it. I then ran this:
Code: Select all
exec("C:\\websafe\\mspaint.exe");

with the same result.

Finally, I tried moving mspaint to my web root, and that didn't work either.

I've installed XAMPP on a standard desktop computer running Windows7, not a windows server. From the discussion on the PHP documentation, I keep seeing IIS, or some such that appears connected to running windows server. Hopefully that's not the issue...

Thanks for any help,
John
ZumJ
 
Posts: 12
Joined: 18. January 2011 04:03

Re: Exec and shell_exec not working

Postby ZumJ » 19. January 2011 21:53

Also, all of these work from the command line.

John
ZumJ
 
Posts: 12
Joined: 18. January 2011 04:03

Re: Exec and shell_exec not working

Postby ZumJ » 20. January 2011 15:36

So I made some progress. Following the recommendation by rymfaxe on the PHP documentation website for the "Program Execution Functions" (one level up from System(), exec(), etc.), I went into services.msc, and changed the security properties of the Apache service to enable interaction with the desktop.
http://www.php.net/manual/en/ref.exec.php#59361

Now when I run mspaint.exe, it works. It appears I am having some sort of syntax, or other difficulty with my pdftk command though since it's telling me:
The filename, directory name, or volume label syntax is incorrect. Still working on that.

John
ZumJ
 
Posts: 12
Joined: 18. January 2011 04:03

Re: Exec and shell_exec not working

Postby peterwt » 20. January 2011 19:16

Hi John,

Glad to hear that you are making progress.

I did not realise you were runnung apache as a service - I run with manual start, not as a service so explains why the paint script worked for me but not for you. Now that you have allowed apache to interact with the desktop the paint script works - this is not needed if you run apache direct as i am.

As you say it looks like a syntax problem. Try this -

Code: Select all
system("pdftk");


or pdftk.exe or full path to pdfk -

Code: Select all
system("C:\\Windows\\System32\\pdftk.exe",$output, $return);


so you can build up to the full requirement. When you can get this to work without errors add the rest of the arguments.

As a further thought have you tried running the full command line in windows console - so you can get the syntax correct there first (only single\).

Peter
Peter
peterwt
 
Posts: 42
Joined: 17. March 2009 11:06
Operating System: Windows 7 64 bit Professional

Re: Exec and shell_exec not working

Postby ZumJ » 20. January 2011 20:06

So it's 100% working now. The big issue was setting the apache service correctly in services.msc The other issue was using two sets of quotes.

This causes the error "The filename, directory name, or volume label syntax is incorrect.":
Code: Select all
$exec_string = ' "pdftk C:\\xampp\\htdocs\\Dev\\docstorage\\activedocs\\documentsId_13_CERT-1001_0.pdf fill_form C:\\websafe\\fdfA81B.tmp output C:\\websafe\\OrderDetailId_0.pdf flatten" ';
exec($exec_string);


This works flawlessly:
Code: Select all
$exec_string = "pdftk C:\\xampp\\htdocs\\Dev\\docstorage\\activedocs\\documentsId_13_CERT-1001_0.pdf fill_form C:\\websafe\\fdfA81B.tmp output C:\\websafe\\OrderDetailId_0.pdf flatten";
exec($exec_string);


This also works just fine:
Code: Select all
$exec_string = 'pdftk docstorage/activedocs/documentsId_13_CERT-1001_0.pdf fill_form "C:\xampp\htdocs\Dev\fdfEC6D.tmp" output docstorage/staticdocs/CofCs/inbox/OrderDetailId_0.pdf flatten';
$exec($exec_string);


Thank you so much for your help and moral support working through this. Never thought that Windows would give me so much grief; liking linux better everyday! Hopefully I threw enough keywords in here that if anyone else runs into this issue, they can find the solution.

John

PS This worked fine on one computer, but when I transferred it to another computer, it did not work, giving the "pdftk is not recognized as an internal or external command, operable program or batch file". Obviously there's some other setting that's crucial that I've been missing. To get it working, I moved the pdftk executable into the PHP folder, and called it directly like this:
Code: Select all
$exec_string = 'C:\xampp\php\pdftk.exe docstorage/activedocs/documentsId_13_CERT-1001_0.pdf fill_form "C:\xampp\htdocs\Dev\fdfEC6D.tmp" output docstorage/staticdocs/CofCs/inbox/OrderDetailId_0.pdf flatten';
$exec($exec_string);

Hope that helps someone :)
ZumJ
 
Posts: 12
Joined: 18. January 2011 04:03


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 131 guests