通过 exec() 使用时 ls 输出发生变化

发布于 2024-11-16 22:28:22 字数 1196 浏览 4 评论 0原文

我通过 PHP 和 exec() 使用 ls 命令,得到的输出与通过 shell 运行相同命令时得到的输出不同。通过 PHP 运行 ls 时,日期的年份和月份将更改为月份名称:

通过 shell 运行命令:

$ ls -lh /path/to/file
 -rw-r--r-- 1 sysadmin sysadmin 36M 2011-05-18 13:25 file

通过 PHP 运行命令:

<?php
exec("ls -lh /path/to/file", $output);
print_r($output);

/*
Array
(
    [0] => -rw-r--r-- 1 sysadmin sysadmin  36M May 18 13:25 file
)
*/

请注意:
-当我通过 cli 运行 PHP 脚本时,不会出现此问题(仅在通过 apache 运行时才会出现此问题)
-我检查了页面的源代码,以确保我所看到的就是我所得到的(并且我确实得到了月份名称而不是正确的日期)
-我还以 www-data 用户身份通过​​ shell 运行 ls 命令,以查看 ls 是否根据用户提供不同的输出( shell 的输出始终相同,即我得到的是 yyyy-mm-dd 中的日期而不是月份名称)

用答案更新

alias 给了我这:

alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

从这些别名中我无法找到直接负责的开关对于时间显示:

-C  list entries by columns
-F  append indicator (one of */=>@|) to entries
-A  do not list implied . and ..
-a  do not ignore entries starting with .
-l  use a long listing format

但是在 PHP 中使用 --time-style=long-iso 确实解决了这个问题。

I'm using the ls command via PHP and exec() and I get a different output than when I run the same command via the shell. When running ls through PHP the year and month of the date get changed into the month name:

Running the command through the shell:

$ ls -lh /path/to/file
 -rw-r--r-- 1 sysadmin sysadmin 36M 2011-05-18 13:25 file

Running the command via PHP:

<?php
exec("ls -lh /path/to/file", $output);
print_r($output);

/*
Array
(
    [0] => -rw-r--r-- 1 sysadmin sysadmin  36M May 18 13:25 file
)
*/

Please note that:
-the issue doesn't occur when I run the PHP script via the cli (it only occurs when run through apache)
-I checked the source code of the page to make sure that what I was seeing was what I was getting (and I do get the month name instead of the proper date)
-I also run the ls command through the shell as the www-data user to see if ls was giving different output depending on the user (the output is the always the same from the shell, that is I get the date in yyyy-mm-dd instead of the month name)

Update with answer

alias was giving me this:

alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

From those aliases I was unable to find a switch that was directly responsible for the time display:

-C  list entries by columns
-F  append indicator (one of */=>@|) to entries
-A  do not list implied . and ..
-a  do not ignore entries starting with .
-l  use a long listing format

However using --time-style=long-iso in PHP did fix the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2024-11-23 22:28:22

ls 有几个用于日期显示格式的命令行选项。检查您的命令行版本是否没有别名以包含诸如 ls --time-style=locale 之类的内容。 PHP exec'd 版本很可能没有此别名,并且使用默认的 ls 设置。

ls has a couple command line options for date display format. check that your command line version isn't aliased to include something like ls --time-style=locale. The PHP exec'd version will most likely not have this aliasing present and is using default ls settings.

多像笑话 2024-11-23 22:28:22

ls 输出取决于当前区域设置。当您代表自己从控制台运行它时,它会使用您的区域设置,但用户 www-data 有自己的区域设置(可能与您的不同)。因此,我建议您明确指定区域设置:

exec("LC_TIME=POSIX ls -lh /", $output);

您可以替换您想要使用的区域设置,而不是 POSIX

ls output depends from current locale settings. When you run it from console on behalf yourself it uses your locale settings, but user www-data has own locale settings (which probably differ from your). So, I suggest to you specify locale settings explicitly:

exec("LC_TIME=POSIX ls -lh /", $output);

where instead of POSIX you may substitute locale which you want to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文