【问题标题】:What is the best method to read a folder sub directory and files in those sub directory?读取文件夹子目录和这些子目录中的文件的最佳方法是什么?
【发布时间】:2012-04-06 04:16:35
【问题描述】:

我有一个图像文件夹,其中包含每个图像专辑的子目录,例如

  • 图片
  • 图片/相册1
  • 图片/相册2

在 PHP 文件中 我使用 GLOB 的相册缩略图为每个相册创建一个链接,以读取图像下的所有文件夹

$dir=glob('images/*');
$dir_listing=array();
foreach($dir as $list)  
{
if(is_dir($list))
$dir_listing[]= (basename($list));
}   

$thumbs=glob('images/thumbnails/*');
$count=0;

foreach($thumbs as $th )
{
echo" $dir_listing <br/>"; 
echo"<a href='$dir_listing[$count]' ><img src='$th' /> </a>";
    $count++;
}

我在每个页面加载时使用 Glob 来获取目录和图像列表。

  • 我想知道是否有更好的方法来做到这一点。
  • 我还想根据 Last Modified 时间降序获取所有文件和文件夹的列表{最新文件和文件夹优先}。

使用 Glob 是否正确,还是应该将子目录和文件保存在文本文件中并从中读取?

【问题讨论】:

    标签: php glob


    【解决方案1】:

    我无法确定是否有更好的方法来执行此操作,但您的代码肯定可以工作。

    仅当目录中的文件数量相对较少(

    if ($handle = opendir($path)) {
    while (false !== ($file = readdir($handle))) {
        // do something with the file
        // note that '.' and '..' is returned even
    }
    closedir($handle);
    

    最后,在 glob() 上使用标志 GLOB_NOSORT,这样最终结果就像它在目录中列出的一样,以防根据上次修改日期为您提供结果。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-08
      • 2013-02-28
      • 2013-10-26
      • 1970-01-01
      • 2015-02-19
      • 2014-11-10
      • 2015-01-23
      • 1970-01-01
      相关资源
      最近更新 更多