所以我在本地工作,但它不在我的服务器上工作。问题是由于我使用scp
命令将我的文件传输到服务器吗?
这是我的代码:
// Local
if ('localhost' == $_SERVER['HTTP_HOST']) {
$cmd = 'find resume*.pdf -type f -print0 | xargs -0 stat -f "%m %N" |sort -rn | head -1 | cut -f2- -d" "';
// Production
} else {
$cmd = 'find resume*.pdf -type f -print0 | xargs -0 ls -drt | tail -n 1';
}
$results = exec($cmd);
echo '$results = ' . $results;
本地输出:
$ php -f index.php
$ $results = resume june 2014.pdf
远程输出:
$ php -f index.php
$ $results = resume_may_2014.pdf
这是我查看服务器上文件的修改日期时的样子。 我也无法弄清楚为什么他们(如同具有相同修改日期的简历文件中)以这种方式订购。它们不像按字母顺序排序,文件大小等。
<!-- language: lang-bash -->
username@username.com [~/www/resume]# ls -lt
total 432
drwxr-xr-x 6 username username 4096 Jun 1 14:05 ./
-rw-r--r-- 1 username username 927 Jun 1 14:00 index.php
-rw-r--r-- 1 username username 2028 Jun 1 13:55 error_log
-rw-r--r-- 1 username username 135855 Jun 1 13:37 resume_may_2014.pdf
-rw-r--r-- 1 username username 0 Jun 1 13:37 resume_feb_2014.pdf
-rw-r--r-- 1 username username 118698 Jun 1 13:37 resume\ june\ 2014.pdf
drwxr-xr-x 4 username username 4096 Jun 1 13:18 resume\ june\ 2014.pages/
-rw-r--r-- 1 username username 135855 May 31 18:59 resume.pdf
参考文章:
答案 0 :(得分:1)
为什么不在PHP中这样做?
<?php
$directory = '/path/to/directory';
$filename = null;
if ($handle = opendir($directory)) {
while (false !== ($entry = readdir($handle))) {
// if (!is_dir($entry)) { // no directories, only files. (Otherwise exclude directories . and ..)
if (substr($entry,0,6) == 'resume' && substr($entry,-4) == '.pdf') { // only resume*.pdf
if ($filename === null || $time < filectime($entry)) { // or use filemtime() for last modified time in stead of last change time
$time = filectime($entry);
$filename = $entry;
}
}
}
if ($filename === null) {
echo "No files found";
} else {
echo "Last changed file is: " . $filename . ". It was last changed at: " . date('r', $time) . ".";
}
closedir($handle);
} else {
echo "Could not open directory";
}
?>