1

I do have a webcam, which uploads images every 30sec into folders on my ftp Server. The images are placed into subfolders (ex. 2020 -> 06 -> 19).

I want to list them with php into a gallery slider - where the newest image is placed first.

I found this code online, unfortunately, my experience in php isn't really good. The code works for me, but i just need an option for it to sort the images date-wise (newest images first).

Could anybody help me with that?

thank you!!

    <?php
// file name: list_pics.php

global $startDir;

/**
 * List Directories function, which transverses sub-folders
 * listing out the image files that exists within it.
 */
function listDir( $path ) {
  global $startDir;
  $handle = opendir( $path );
  while (false !== ($file = readdir($handle))) {
    if( substr( $file, 0, 1 ) != '.' ) {
      if( is_dir( $path.'/'.$file ) ) {
        listDir( $path.'/'.$file );
      }
      else {
        if( @getimagesize( $path.'/'.$file ) ) {

          /*
          // Uncomment if using with the below "pic.php" script to
          // encode the filename and protect from direct linking. 
          $url = 'http://domain.tld/images/pic.php?pic='
                 .urlencode( str_rot13( substr( $path, strlen( $startDir )+1 ).'/'.$file ) );
          */

          $url='http://domain.tld/images/'. 
          substr( $path, strlen( $startDir )+1 ).'/'.$file;

          // You can customize the output to use img tag instead.
          echo "<a href='".$url."'>".$url."</a><br>";
        }
      }
    }
  }
  closedir( $handle );
} // End listDir function

$startDir = '.';
listDir( $startDir );

?>
pauschpage
  • 11
  • 1
  • See this https://stackoverflow.com/questions/2667065/sort-files-by-date-in-php. You need two for loops. One to get all images in array, then sort them and use sorted array to generate HTML. – Sameer Naik Jun 19 '20 at 18:38
  • Hi Sameer! Thanks for helping. Unfortunately, i dont really know how to do that, my know-how in php is not really well :-( i was kinda looking for something "already existing". – pauschpage Jun 20 '20 at 05:48
  • Please accept the answer if the solution worked for you. Thanks! – Sameer Naik Jun 22 '20 at 23:59

1 Answers1

1

Please see if this works for you.

<?php
// file name: list_pics.php

global $startDir;
$fileList = [];

/**
 * List Directories function, which transverses sub-folders
 * listing out the image files that exists within it.
 */
function listDir( $path ) {
  global $startDir;
  $handle = opendir( $path );
  global $fileList;
  while (false !== ($file = readdir($handle))) {
    if( substr( $file, 0, 1 ) != '.' ) {
      if( is_dir( $path.'/'.$file ) ) {
        listDir( $path.'/'.$file );
      }
      else {
        if( !empty(@getimagesize( $path.'/'.$file ))) {
          array_push($fileList, $path.'/'.$file);
        }
      }
    }
  }
  closedir( $handle );
}

function createLinks($fileList) {

    foreach ($fileList as $filePath) {
      /*
      // Uncomment if using with the below "pic.php" script to
      // encode the filename and protect from direct linking.
      $url = 'http://domain.tld/images/pic.php?pic='
            .urlencode( str_rot13( substr( $path, strlen( $startDir )+1 ).'/'.$file ) );
      */

      $url='http://domain.tld/images/'.
      substr( $filePath, strlen( $startDir )+1 ).'/'.$file;

      // You can customize the output to use img tag instead.
      echo "<a href='".$url."'>".$url."</a><br>";
  }
}

$startDir = '.';
listDir( $startDir );
createLinks($fileList);
Sameer Naik
  • 1,326
  • 1
  • 13
  • 28