如何在PHP Shell_exec中正确处理空格?

时间:2022-11-29 14:17:02

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

我通过cmd-line运行win2003服务器,PHP 526。

I have a cmdline string:

我有一个cmdline字符串:

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

Trying to do this in php code

试图在PHP代码中执行此操作

$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".

foo.exe导致失败,因为它将-p arg解释为“d:\ data”。

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

但是,复制到windows shell cmdline的相同$ cdm字符串会成功执行。

How do I properly handle spaces in PHP shell_exec?

如何在PHP shell_exec中正确处理空格?

4 个解决方案

#1


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).

使用escapeshellarg()来转义你的参数,它应该用你的平台的引号和转义空格的适当组合来逃避它(我猜你是在Windows上)。

#2


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:

与Unix不同,它需要引号内的引号才能被转义,Windows似乎不会以这种方式工作。它如何跟踪报价是超出我的,但这实际上似乎有效,尽管所有相反的逻辑:

$cmd = '" "C:\Path\To\Command" "Arg1" "Arg2" "';
$fp = popen($cmd, 'r');
$output='';
while ($l = fgets($fp, 1024))
$output.=$l;

$ cmd ='“”C:\ Path \ To \ Command“”Arg1“”Arg2“”'; $ fp = popen($ cmd,'r'); $输出= ''; 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!

我猜测command.exe正在使用命令字符串total并且修改(否则是多余的)外部引号。如果没有这些外部引用,DOS会做一些奇怪的事情。这个解决方案与用户187383类似,但是不使用“cmd / c”,它只是模糊了实际发生的事情以及温和的性能削减,因为cmd / c首先是shell调用所隐含的!

#3


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.

这是一个有趣的问题。显然,PHP允许你在程序或参数周围加上双引号,但不能同时使用两者。可能值得将此报告为错误。

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

解决方法是使用DOS 8.3名称而不是引号。例如,“C:\ Program Files \”通常变为“C:\ Progra~1”。

#4


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:

也有这个问题 - 提出了通过cmd.exe路由启动的想法。这里的诀窍是不要迷失在双重qoutes中。通常,您希望放置要运行的任何内容:

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

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

其中$ path是可执行文件的双引号路径。例:

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

#1


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).

使用escapeshellarg()来转义你的参数,它应该用你的平台的引号和转义空格的适当组合来逃避它(我猜你是在Windows上)。

#2


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:

与Unix不同,它需要引号内的引号才能被转义,Windows似乎不会以这种方式工作。它如何跟踪报价是超出我的,但这实际上似乎有效,尽管所有相反的逻辑:

$cmd = '" "C:\Path\To\Command" "Arg1" "Arg2" "';
$fp = popen($cmd, 'r');
$output='';
while ($l = fgets($fp, 1024))
$output.=$l;

$ cmd ='“”C:\ Path \ To \ Command“”Arg1“”Arg2“”'; $ fp = popen($ cmd,'r'); $输出= ''; 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!

我猜测command.exe正在使用命令字符串total并且修改(否则是多余的)外部引号。如果没有这些外部引用,DOS会做一些奇怪的事情。这个解决方案与用户187383类似,但是不使用“cmd / c”,它只是模糊了实际发生的事情以及温和的性能削减,因为cmd / c首先是shell调用所隐含的!

#3


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.

这是一个有趣的问题。显然,PHP允许你在程序或参数周围加上双引号,但不能同时使用两者。可能值得将此报告为错误。

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

解决方法是使用DOS 8.3名称而不是引号。例如,“C:\ Program Files \”通常变为“C:\ Progra~1”。

#4


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:

也有这个问题 - 提出了通过cmd.exe路由启动的想法。这里的诀窍是不要迷失在双重qoutes中。通常,您希望放置要运行的任何内容:

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

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

其中$ path是可执行文件的双引号路径。例:

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