tags:

views:

535

answers:

5

I'm running on win2003 server, PHP 526, via the cmd-line.

I have a cmdline string:

$cmd = '  "d:\Prog Files\foo.exe" -p "d:\data path\datadir"  ';

Trying to do this in php code

$out = `$cmd`;       # note use of backticks AKA shell_exec

results in a failure by foo.exe as it interprets the -p arg as "d:\data".

However, the same $cdm string copied to the windows shell cmdline executes successfully.

How do I properly handle spaces in PHP shell_exec?

+3  A: 

Use escapeshellarg() to escape your arguments, it should escape it with an appropriate combination of quotation marks and escaped spaces for your platform (I'm guessing you're on Windows).

Ciaran McNulty
This is the answer you are looking for :)
Jay
Don't you just love those function naming consistencies in PHP? :-/
Artem Russakovskii
A: 

I'm having the same problem.

As soon as I put double quotes around an argument after the path (c:/program files/path/to) I get the error 'C:\Program' is not recognized as an internal or external command, operable program or batch file.

I tried escaping the ' and/or ", using escapeshellarg() but nothing seems to help.

Does somebody knows what the problem is?

A: 

exactly same problem but i want to run something on "C:\documents and settings\user\desktop\folder\exe.exe"

'C:\Documents' is not recognized as an internal or external command

A: 

This is an interesting problem. Apparently, PHP lets you put double quotes around the program or the arguments, but not both. It may be worth reporting this as a bug.

A work around is to use the DOS 8.3 name instead of quotes. E.g., "C:\Program Files\" usually becomes "C:\Progra~1".

nate
A: 

Had this problem too - came up with an idea to route the launching through cmd.exe. The trick here is not to get lost in the double qoutes. Generally you want to put anything you want to run in:

exec('cmd /c " '.$path.' "';

Where $path is a already double-quoted path to your executable. Example:

$path = '"C:\Program Files\ToDoList Simple\ToDoList.exe" "C:\SomePath\todo.tdl" -nt test -cm test2';