-1

I have a php file which displays videos from a folder named gallery but the problem is it displays videos in alphabetical order and I want the videos to be displayed by date i.e newest first. What do I need to add in my php code?

Here's the code:

<div id="vid-gallery">
   <?php
   $exten = '.mp4';
     $dir = __DIR__ . DIRECTORY_SEPARATOR . "gallery" . DIRECTORY_SEPARATOR;
     $videos = glob("$dir*.{mp4}", GLOB_BRACE);
   $alt = glob("$dir*.{webm,mp4,ogg}", GLOB_BRACE);
     if (count($videos) > 0) { foreach ($videos as $vid) {
        $base = basename($vid,'.mp4');
       printf("<div class='vi'><video src='gallery/%s' id='basename($vid,'.mp4')' loop='loop'></video>", rawurlencode(basename($vid)));
     echo "<div class='title'>".basename($vid,'.mp4')."</div></div>";
}}
  ?>
  </div>

When answering please tell where should the answered code be put.

You can check example the here:

https://victure.repl.co

And please it isn't the same as sorting arrays, it's different.

Thanks in advance:)

TJBro
  • 9
  • 3
  • 2
    Does this answer your question? [How to sort files by date in PHP](https://stackoverflow.com/questions/2667065/how-to-sort-files-by-date-in-php) – AymDev Apr 05 '22 at 15:41
  • 2
    *"And please it isn't the same as sorting arrays, it's different."* well, it ***is*** array sorting. – AymDev Apr 05 '22 at 15:42
  • In your other (duplicate) post someone suggested using [filectime](https://www.php.net/manual/en/function.filectime.php), that didn't work out? – brombeer Apr 05 '22 at 16:01

1 Answers1

0

U can do this:

  1. Use glob for take all that mach (example al .avi)
  2. Use usort to expecify how sort your file array
  3. Inside usort use filemtime for see the last write date.

Use this:

$files = glob('path/to/files/*.avi');
    usort($files, function($file1, $file2) {
    return filemtime($file2) - filemtime($file1);
});

for echo use this:

foreach($files as $file){
    printf(basename($file).' '.date('F d Y, H:i:s', filemtime($file)));
}
Voxxii
  • 174
  • 1
  • 6