Составление списка файлов изображений в папках и подпапках с PHP и их сортировка по дате создания - PullRequest
1 голос
/ 19 июня 2020

У меня есть веб-камера, которая загружает изображения каждые 30 секунд c в папки на моем ftp-сервере. Изображения помещаются в подпапки (например, 2020 -> 06 -> 19).

Я хочу перечислить их с помощью php в слайдере галереи - где самое новое изображение помещается первым.

Я нашел этот код в Интернете, к сожалению, мой опыт работы с php не очень хорош. Код работает для меня, но мне просто нужна опция для сортировки изображений по дате (сначала самые новые изображения).

Может ли кто-нибудь мне помочь с этим?

спасибо !!

    <?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 );

?>

1 Ответ

1 голос
/ 21 июня 2020

Пожалуйста, посмотрите, работает ли это для вас.

<?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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...