简体   繁体   中英

php listing a directory files by first the newest

Here is the thing I am trying to create a CMS blog based in files.

And to display the posts i need te get the 10 latest files in a folder.

I found scandir , but i do not what it to have to read all the files, and then sort them because, then the time it take to process dependes on the number of files.

So i found opendir , this way i could go through the folder file by file but, as the documentation of opendir tells, it uses the order of the file system wich in linux is based on timestamp but first comes the latest file modified.

So what i ask is there a way to change how linux orders the files in a folder. Or is there a way to do what i want?

看一下: http : //php.net/manual/en/function.filemtime.php ,在用户注释中有一个示例,用于获取最新文件,例如,您可以修改该文件以根据文件的最新情况返回10个最新文件。 mtime。

I think I can help you, but the code I have will list all files instead of only the last 20, easy to add that and tis code will list newest to oldest:

<?PHP

$directory="photos";
$sortOrder="newestFirst";

   $results = array();
   $handler = opendir($directory);

while ($file = readdir($handler)) { 
       if ($file != '.' && $file != '..' && $file != "robots.txt" && $file != ".htaccess"){
           $currentModified = filectime($directory."/".$file);
           $file_names[] = $file;
           $file_dates[] = $currentModified;
       }   
   }
       closedir($handler);

   //Sort the date array by preferred order
   if ($sortOrder == "newestFirst"){
       arsort($file_dates);
   }else{
       asort($file_dates);
   }

   //Match file_names array to file_dates array
   $file_names_Array = array_keys($file_dates);
   foreach ($file_names_Array as $idx => $name) $name=$file_names[$name];
   $file_dates = array_merge($file_dates);

   $i = 0;

   //Loop through dates array and then echo the list
   foreach ($file_dates as $$file_dates){
       $date = $file_dates;
       $j = $file_names_Array[$i];
       $file = $file_names[$j];
       $i++;

       echo  "<img src=photos/$file>\n";       
   }

?> 

Hope that helps you.

cheers

If you have a folder with lots of file, and worry about the performance of the scandir,
you can consider to setup a cronjob to do background scandir + sorting,
make sure you store the result into another storage like memcache /database / disk-file

Let's said you cronjob is running every 5 minute,
your cronjob script should overwrite the results on the storage
only upon success (after end of the scandir + sorting)

So, your CMS script is just to read from the storage,
or if results from storage is not found,
then proceed with the scandir + sorting

Usually the first user hit the page and the cache is not ready, he will get a very slow response,
that's the reason to use a cronjob is to do the pre-cache

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM