2011-02-25 151 views
0

定義多個文件我需要執行一個適用於多個文件的exec命令,但它不起作用。我將創建一個示例來展示這一點。很顯然,我並不需要此目錄的任何東西,我做的「ls」命令的例子,雖然與其他任何命令發生:php exec命令不允許使用{}

exec("ls -al /etc/security/", $output); 
print_r($output); 

Array 
(
    [0] => total 40 
    [1] => drwxr-xr-x 2 root root 4096 Jan 27 2010 . 
    [2] => drwxr-xr-x 83 root root 4096 Feb 24 12:38 .. 
    [3] => -rw-r--r-- 1 root root 4266 Apr 9 2008 access.conf 
    [4] => -rw-r--r-- 1 root root 3551 Apr 9 2008 group.conf 
    [5] => -rw-r--r-- 1 root root 1911 Apr 9 2008 limits.conf 
    [6] => -rw-r--r-- 1 root root 1507 Apr 9 2008 namespace.conf 
    [7] => -rwxr-xr-x 1 root root 977 Apr 9 2008 namespace.init 
    [8] => -rw------- 1 root root 0 Jan 27 2010 opasswd 
    [9] => -rw-r--r-- 1 root root 2980 Apr 9 2008 pam_env.conf 
    [10] => -rw-r--r-- 1 root root 2180 Apr 9 2008 time.conf 
) 

但隨着更多文件嘗試...

exec("ls -al /etc/security/{access.conf,group.conf}", $output); 
print_r($output); 

ls: cannot access /etc/security/{access.conf,group.conf}: No such file or directory 

當然這作品在控制檯:

$ ls -al /etc/security/{access.conf,group.conf} 
-rw-r--r-- 1 root root 4266 2008-04-09 15:25 /etc/security/access.conf 
-rw-r--r-- 1 root root 3551 2008-04-09 15:25 /etc/security/group.conf 
+1

我認爲問題在於'{}'被shell **而不是'ls'命令擴展**。 PHP的'exec'不會穿過一個shell,或者至少不是同一種shell,因此不會擴展速記。 – deceze 2011-02-25 01:58:29

+0

所以...無論如何解決它? ;) – FlamingMoe 2011-02-25 02:03:12

回答

1

正如評論所說,這是shell擴展{}簡寫,它並不是在PHP ENVIRON發生的問題換貨。你必須單獨提供完整的路徑:

exec('ls -al /etc/security/access.conf /etc/security/group.conf', $output); 
1

如果你有這樣做的權利,這可能是簡單地使猛砸提供商/ bin/sh的的問題。