php exec 命令不允许使用 { } 定义多个文件
我需要执行一个适用于多个文件的 exec 命令,但它不起作用。我将创建一个示例来展示这一点。显然,我不需要这个目录的任何内容,并且我使用“ls”命令执行示例,尽管与任何其他命令一起发生:
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
I need to do an exec command that applies to several files, but it's not working. I'm going to create an EXAMPLE to show this. Obviously, I don't need anything of this directory and I do the example with "ls" command, although happens with any other command:
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
)
But trying with more files ...
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
Of course this works in console:
$ 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如评论中提到的,这是 shell 扩展
{}
简写的问题,在 PHP 环境中不会发生这种情况。您必须单独提供完整路径:As mentioned in the comments, this is a problem of the shell expanding the
{}
shorthand, which does not happen in a PHP environment. You'll have to supply the full paths separately:如果您有权这样做,那么这可能只是让 bash 成为 /bin/sh 的提供者的问题。
If you have the rights to do so, this is probably simply a matter of making bash the provider for /bin/sh.