Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在当前目录中查找最近添加的文件_Php_Bash_Shell_Scp_Command Line Interface - Fatal编程技术网

Php 在当前目录中查找最近添加的文件

Php 在当前目录中查找最近添加的文件,php,bash,shell,scp,command-line-interface,Php,Bash,Shell,Scp,Command Line Interface,所以我让它在本地工作,但在我的服务器上不工作。问题是否是由于我使用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 { $cm

所以我让它在本地工作,但在我的服务器上不工作。问题是否是由于我使用
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

username@username.com[~/www/resume]#ls-lt
总数432
drwxr-xr-x 6用户名4096 Jun 1 14:05/
-rw-r--r--1用户名927 Jun 1 14:00 index.php
-rw-r--r--1用户名2028年6月1日13:55错误日志
-rw-r--r--1用户名用户名135855 Jun 1 13:37简历_may_2014.pdf
-rw-r--r--1用户名用户名0 Jun 1 13:37简历_feb_2014.pdf
-rw-r--r--1用户名用户名118698 Jun 1 13:37 resume\june\2014.pdf
drwxr-xr-x 4用户名4096 Jun 1 13:18 resume\june\2014.page/
-rw-r--r--1用户名135855五月31日18:59 resume.pdf
引用的文章:


    • 为什么不在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";
      }
      ?>
      
      
      

      文件列表按修改时间排序(最新优先),如您使用
      -t
      选项所要求。谢谢@ooga!请注意我添加的第一部分(在修改日期相同的简历文件中)部分。ie:这些文件是如何排序的:resume\u may\u 2014.pdf、resume\u feb\u 2014.pdf、resume\june\2014.pdf等等。这就是我所困惑的。对不起。我显然没有很好地理解你的问题。在具有相同修改日期的文件组中,它们可能按inode的顺序排列,这不是特别有用。也许创建时间(
      -c
      )比上次修改时间对您更有用。您现在棒极了。我之所以使用bash是因为我想学习更多,但这完全是可行的。非常感谢!