【发布时间】:2011-09-22 02:47:08
【问题描述】:
我正在通过 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 确实解决了这个问题。
【问题讨论】:
-
在 shell 中输入“别名”。也许你在那里有一个 ls 的别名。