繁体   English   中英

使用 PHP 列出文件夹和子文件夹中的图像文件并按创建日期排序

[英]Listing image files in folders and subfolders with PHP and sort them by date created

我有一个网络摄像头,每 30 秒将图像上传到我的 ftp 服务器上的文件夹中。 图像放置在子文件夹中(例如 2020 -> 06 -> 19)。

我想将它们与 php 一起列在画廊 slider 中 - 最新的图像放在第一位。

我在网上找到这段代码,不幸的是,我在 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 ); ?>

请查看这是否适合您。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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