0

I have a script that retrieves files from a directory and need to sort files by date. Latest photos uploaded to the top of the list. Please advise anyone?

<?
$slozka = "./gallery/holiday/"; //select the folder from which you want to list files
$thumb= "thumb"; //the name of the folder thumbnails
$vypis = opendir($slozka); //open folder
$celkem = '0'; //beginning number of photos

while (false!==($file = readdir($vypis))) //reading files
{ 
    if($file!="."&&$file!=".."&&!is_dir($file)&&$file!=$thumb) //search through folder
    { 
        $celkem++; //count the number of pictures
        $filetitle = $file;
        $nahrada = array("_", ".jpg", ".png", ".gif");
        $filetitle = str_replace($nahrada, " ", "$filetitle");

        if (file_exists($slozka.$thumb.'/'.$file))
        { //If there is a preview and display it ..
            echo "<li><a href=\"gallery/holiday/".$file."\" alt=\"".$file."\"
title=\"".$filetitle."\" class=\"holiday\" /><img src=\"gallery/holiday/thumb/".$file."\" alt=\"".$file."\"></a><span class=\"nazvy\">".$filetitle."</span></li>";
        }//If there is no way it will create ...
        else 
            echo "<li><a href=\"gallery/holiday/".$file."\" alt=\"".$file."\" class=\"holiday\" /><img src=\"thumb.php?nazev=".$file."&amp;cesta=".$slozka."\" alt=\"".$file."\"></a><span class=\"nazvy\">".$filetitle."</span></li>";
    } 
}     
echo "</ul><div id=\"soucet\">Celkem fotek : ".$celkem."</div>"; // print the number of photos in the gallery ...
closedir($vypis); //close folder
?>
kuldeep.kamboj
  • 2,566
  • 3
  • 26
  • 63
Regmoner
  • 1
  • 1
  • 1

2 Answers2

0

You can use the terminal command find function to get the latest file from the directory. Do sort reverse and loop the directory with the file pattern you are checking. You will then get the latest file by the date.

check the link for your reference.

https://superuser.com/questions/294161/unix-linux-find-and-sort-by-date-modified

Ex :

find . -type f -name '{$fileFormat}*'.txt -exec ls -tr {} \; | sort -r | head -10
$output = array();
@chdir($destinationDir);
@exec($command, $output);

Then loop the $output.

Community
  • 1
  • 1
sandy
  • 1,126
  • 1
  • 7
  • 17
0

You should have searched for this.

It's pretty obvious natively that opendir() does not retrieve file modification, nor creation dates.

A simple search for "file modified last php" will redirect your here:

https://www.google.com/search?q=php+file+modified+last

where the first result is php.net's doc:

http://php.net/filemtime

Where you can use

date ("F d Y H:i:s.", filemtime($file)

In order to retrieve a datetime timestamp for your list.

You can build array on the top of it and sort it by one of the array sorting functions (http://www.php.net/manual/en/array.sorting.php)

And last but not least - English is the official programming language, so get rid of this variable naming like $celkem and $slozka

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
  • But here, you need to check for each file and store its modified timestamp which looks a tedious process. Your thoughts ? – sandy Mar 10 '14 at 07:35
  • you do it in your while loop. e.g. `$fileInfo['filename'][] = $file; $fileInfo['modified'][] = date("F d Y H:i:s.", filemtime($file));` – Royal Bg Mar 10 '14 at 07:44
  • So `$fileInfo` will become an array containing filenames and modified – Royal Bg Mar 10 '14 at 07:45
  • So here we have to loop twice one to get the list of files by date and then loop again ($fileInfo) to read them. – sandy Mar 10 '14 at 07:49
  • Yep, this is how I see it. You can try googling a different approach. If there's better one, you can use it. To be honest, looping twice is not that bad, if you do not duplicate code. If this case you are not. Once you loop to build the necessary array. And twice to extract what you need to the end-user. – Royal Bg Mar 10 '14 at 07:51
  • Thanks for the tip. I tried, but still it does not work or I do not know where I insert. – Regmoner Mar 12 '14 at 16:52