How to sort files by date modified via php

Reference: I have an anonymous ftp server, ftp_nlist only list files in alphabetical order, I would like to get a list of files based on the last modified date, recent first. I tried ftp_exec ($ conn, "ls -t"), but I got a failure permission error, I don’t know why it doesn’t work. Well, I work with php-cli, and the number of files in thousands, I just want to work with recent files. Getting the source list and finding the date element of the array elements can help, but I hope there is a simple way out. When I log in through the terminal command, ls -t works fine. Therefore, I wonder why ftp_exec does not work.

Look for an easy offer.

Thanks in advance.

+3
source share
3 answers

If you want to sort files by last modified date, you can use

ftp_nlist($conn, '-t .');

This will not tell you what date is for each file.

If you want to also get the modified date, you can use ftp_rawlistand analyze the output. Here is a quick example that I put together:

$list = ftp_rawlist($ftp, '.');

$results = array();
foreach ($list as $line) {
    list($perms, $links, $user, $group, $size, $d1, $d2, $d3, $name) =
        preg_split('/\s+/', $line, 9);
    $stamp = strtotime(implode(' ', array($d1, $d2, $d3)));
    $results[] = array('name' => $name, 'timestamp' => $stamp);
}

usort($results, function($a, $b) { return $a['timestamp'] - $b['timestamp']; });

At this point, it $resultscontains a list sorted in ascending order of the last modified time; cancel the sort function to get the list in the last modified first format.

Note. ftp_rawlistdoes not provide exact timestamps for modification, so this code may not always work exactly. You should also make sure that the output from your FTP server is consistent with this algorithm and includes some sanity checks to ensure that this will be the case in the future.

+14

, . , URL ftp://, allow_url_fopen ( DIR_SORT_ATIME DIR_SORT_CTIME ):

// Constants to make usage more reader-friendly
define('DIR_SORT_NAME',  1);
define('DIR_SORT_SIZE',  2);
define('DIR_SORT_ATIME', 3);
define('DIR_SORT_MTIME', 4);
define('DIR_SORT_CTIME', 5);

function readdir_sorted_array ($dir, $sortCol = DIR_SORT_NAME, $sortDir = SORT_ASC) {

  // Validate arguments
  $dir = rtrim(str_replace('\\', '/', $dir), '/');
  $sortCol = (int) ($sortCol >= 1 && $sortCol <= 5) ? $sortCol : 1;
  $sortDir = ($sortDir == SORT_DESC) ? SORT_DESC : SORT_ASC;
  $name = $size = $aTime = $mTime = $cTime = $table = array();

  // Open the directory, return FALSE if we can't
  if (!is_dir($dir) || (!$dp = opendir($dir))) return FALSE;

  // Fetch a list of files in the directory and get stats
  for ($i = 0; ($file = readdir($dp)) !== FALSE; $i++) {
    if (!in_array($file, array('.','..'))) {
      $path = "$dir/$file";
      $row = array('name'=>$file,'size'=>filesize($path),'atime'=>fileatime($path),'mtime'=>filemtime($path),'ctime'=>filectime($path));
      $name[$i] = $row['name'];
      $size[$i] = $row['size'];
      $aTime[$i] = $row['atime'];
      $mTime[$i] = $row['mtime'];
      $cTime[$i] = $row['ctime'];
      $table[$i] = $row;
    }
  }

  // Sort the results
  switch ($sortCol) {
    case DIR_SORT_NAME:
      array_multisort($name, $sortDir, $table);
      break;
    case DIR_SORT_SIZE:
      array_multisort($size, $sortDir, $name, SORT_ASC, $table);
      break;
    case DIR_SORT_ATIME:
      array_multisort($aTime, $sortDir, $name, SORT_ASC, $table);
      break;
    case DIR_SORT_MTIME:
      array_multisort($mTime, $sortDir, $name, SORT_ASC, $table);
      break;
    case DIR_SORT_CTIME:
      array_multisort($cTime, $sortDir, $name, SORT_ASC, $table);
      break;
  }

  // Return the result
  return $table;

}

$dir.

+1

It seems that the “ftp_nlist” second arg takes the same arguments as the linux command “ls” (the OP shouldn't have used “ls”, just switches)

$dir_list = ftp_nlist($ftp_conn_id, '-rtla .');
print_r($dir_list);
0
source

All Articles