문제

I'm trying to write a function that returns and array of the files names ordered by time modified. Although I want to get only a specific number of files, and not the whole files in the directory. In conclusion, I'd like to get an array that contains the newest X files from a directory.

This is my code:

    public static function GetPicsDir()
    {
        $results = array();
        $handler = opendir("pics");
        while ($file = readdir($handler)) {
          if ($file != "." && $file != "..") {
            $results[] = $file;
          }
        }
        closedir($handler);
        return $results;
    }

I don't know how to limit it and order by time modified. I'd be glad to get any help.

Thank you

도움이 되었습니까?

해결책 2

You can use glob

$date = strtotime('2013-01-10 10:00:00');//The date from you want to get the files
$matches = glob('dir/*.*');
$result=array();
if (is_array($matches)) {
  $a=0;
  foreach ($matches as $filename) {
    if (filemtime($filename) >= $date) {//only output file >= your $date
      $result[$a]['FileName'] = $filename;
      $result[$a]['DateCreated'] = gmdate("Y-m-d H:i:s", filemtime($filename));
    }
      $a++;
  }
}

if(count($result)>=2){//order array if it has at least 1 match
   foreach ($result as $key => $row) {
      $new_array[$key] = $row['DateCreated'];
    }
 array_multisort($new_array,SORT_DESC,$result);//Show most recent first 
}

using array_multisort to sort by dates SORT_DESC

echo '<pre>';
print_r($result);
echo '<pre>';

Output:

Array
(
  [0] => Array
      (
          [FileName] => test.php
          [DateCreated] => 2013-10-20 05:43:06
      )

  [1] => Array
      (
          [FileName] => test.sql
          [DateCreated] => 2013-09-20 23:38:05
      )

  [2] => Array
      (
          [FileName] => general.php
          [DateCreated] => 2013-09-02 00:58:33
      )

)

다른 팁

Use filemtime():

public static function GetPicsDir()
{
    $results = array();
    $handler = opendir("pics");
    while ($file = readdir($handler)) {
      if ($file != "." && $file != "..") {
        $results[$time] = filemtime($file);
      }
    }
    arsort($results);
    closedir($handler);
    return $results;
}

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

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

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

If you want to get the modified date as well, you can use ftp_rawlist and parse the output. Here's a quick example I scraped 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 $results contains a list sorted in ascending last modified time; reverse the sort function to get the list in most recently modified first format.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top