【问题标题】:Listing image files in folders and subfolders with PHP and sort them by date created使用 PHP 列出文件夹和子文件夹中的图像文件,并按创建日期对它们进行排序
【发布时间】:2020-06-19 17:51:54
【问题描述】:

我确实有一个网络摄像头,它每 30 秒将图像上传到我的 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 );

?>

【问题讨论】:

  • stackoverflow.com/questions/2667065/sort-files-by-date-in-php。你需要两个 for 循环。一种是获取数组中的所有图像,然后对它们进行排序并使用排序后的数组生成 HTML。
  • 嗨萨米尔!感谢您的帮助。不幸的是,我真的不知道该怎么做,我在 php 方面的专业知识不是很好:-( 我有点在寻找“已经存在”的东西。
  • 如果解决方案对您有效,请接受答案。谢谢!

标签: php


【解决方案1】:

请看看这是否适合你。

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

【讨论】:

    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 2012-02-16
    • 2015-01-10
    • 2023-01-11
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多