|How do I to properly handle spaces in PHP Shell_exec?||#1|
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
string copied to the windows shell cmdline executes successfully.
How do I properly handle spaces in PHP
posted date: 2008-12-18 09:00:00
|Re: How do I to properly handle spaces in PHP Shell_exec?||#3|
the formatting of your question is off. use 4 spaces in front of code samples!
posted date: 2008-12-18 09:07:00
|Re: How do I to properly handle spaces in PHP Shell_exec?||#4|
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).
posted date: 2008-12-18 09:42:00
|Re: How do I to properly handle spaces in PHP Shell_exec?||#5|
This is the answer you are looking for :)
posted date: 2008-12-19 04:48:00
|Re: How do I to properly handle spaces in PHP Shell_exec?||#6|
Don't you just love those function naming consistencies in PHP? :-/
posted date: 2009-05-23 21:07:00
|Re: How do I to properly handle spaces in PHP Shell_exec?||#7|
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".
posted date: 2009-10-06 18:38:00
|Re: How do I to properly handle spaces in PHP Shell_exec?||#8|
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';
posted date: 2009-10-09 11:01:00
|Re: How do I to properly handle spaces in PHP Shell_exec?||#9|
Unlike Unix, which requires quotes within quotes to be escaped, Windows doesn't seem to work this way. How it keeps track of the quotes is beyond me, but this actually seems to work, despite all logic to the contrary: $cmd = '" "C:\Path\To\Command" "Arg1" "Arg2" "';$fp = popen($cmd, 'r');$output='';while ($l = fgets($fp, 1024)) $output.=$l; I'm guessing command.exe is taking the command string total and nixing the (otherwise redundant) outside quotes. Without these outside quotes, DOS does some weird things. This solution is similar to post by user187383, but does away with the "cmd /c" which only obfuscates what's actually happening along with a mild performance cut, since cmd /c is implicit by the shell call in the first place!
posted date: 2011-02-22 15:40:00
|select page: « 1 »|