【问题标题】:Sort and display directory list alphabetically using opendir() in php使用 php 中的 opendir() 按字母顺序排序和显示目录列表
【发布时间】:2010-10-27 11:09:35
【问题描述】:

php noob here - 我拼凑了这个脚本以显示来自 opendir 文件夹的图像列表,但我无法确定如何(或在哪里)按字母顺序对数组进行排序

<?php

// opens images folder
if ($handle = opendir('Images')) {
while (false !== ($file = readdir($handle))) {

// strips files extensions  
$crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");    

$newstring = str_replace($crap, " ", $file );   

//asort($file, SORT_NUMERIC); - doesnt work :(

// hides folders, writes out ul of images and thumbnails from two folders

    if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
    echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";}
}
closedir($handle);
}

?>

任何建议或指示将不胜感激!

【问题讨论】:

    标签: php sorting directory


    【解决方案1】:

    使用opendir()

    opendir() 不允许对列表进行排序。您必须手动执行排序。为此,首先将所有文件名添加到数组中,然后使用sort() 对其进行排序:

    $path = "/path/to/file";
    
    if ($handle = opendir($path)) {
        $files = array();
        while ($files[] = readdir($dir));
        sort($files);
        closedir($handle);
    }
    

    然后然后使用foreach列出它们:

    $blacklist = array('.','..','somedir','somefile.php');
    
    foreach ($files as $file) {
        if (!in_array($file, $blacklist)) {
            echo "<li>$file</a>\n <ul class=\"sub\">";
        }
    }
    

    使用scandir()

    使用scandir() 会容易得多。默认情况下,它会为您执行排序。使用以下代码可以实现相同的功能:

    $path = "/path/to/file";
    $blacklist = array('somedir','somefile.php');
    
    // get everything except hidden files
    $files = preg_grep('/^([^.])/', scandir($path)); 
    
    foreach ($files as $file) {
        if (!in_array($file, $blacklist)) {
            echo "<li>$file</a>\n <ul class=\"sub\">";
        }
    }
    

    使用DirectoryIterator(首选)

    $path = "/path/to/file";
    $blacklist = array('somedir','somefile.php');
    
    foreach (new DirectoryIterator($path) as $fileInfo) {
        if($fileInfo->isDot()) continue;
        $file =  $path.$fileInfo->getFilename();
        echo "<li>$file</a>\n <ul class=\"sub\">";
    }
    

    【讨论】:

    • $blacklist 是干什么用的?
    • 将不需要的物品拒之门外。我更喜欢DirectoryIterator 方式,因为它比解决问题要小得多。
    • @Wilf:它不存在。这只是我复制粘贴时出现的东西。
    • 注意:scandir 排序 DirectoryIterator 没有
    【解决方案2】:

    这就是我会做的方式

    if(!($dp = opendir($def_dir))) die ("Cannot open Directory.");
    while($file = readdir($dp))
    {
        if($file != '.')
        {
            $uts=filemtime($file).md5($file);  
            $fole_array[$uts] .= $file;
        }
    }
    closedir($dp);
    krsort($fole_array);
    
    foreach ($fole_array as $key => $dir_name) {
      #echo "Key: $key; Value: $dir_name<br />\n";
    }
    

    【讨论】:

      【解决方案3】:

      注意:将其移到 foreach 循环中,以便正确重命名 newstring 变量。

      // strips files extensions      
      $crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");    
      
      $newstring = str_replace($crap, " ", $file );
      

      【讨论】:

        【解决方案4】:
        $directory = scandir('Images');
        

        【讨论】:

          【解决方案5】:

          您需要先将文件读入数组,然后才能对它们进行排序。这个怎么样?

          <?php
          $dirFiles = array();
          // opens images folder
          if ($handle = opendir('Images')) {
              while (false !== ($file = readdir($handle))) {
          
                  // strips files extensions      
                  $crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");    
          
                  $newstring = str_replace($crap, " ", $file );   
          
                  //asort($file, SORT_NUMERIC); - doesnt work :(
          
                  // hides folders, writes out ul of images and thumbnails from two folders
          
                  if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
                      $dirFiles[] = $file;
                  }
              }
              closedir($handle);
          }
          
          sort($dirFiles);
          foreach($dirFiles as $file)
          {
              echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";
          }
          
          ?>
          

          编辑:这与您的要求无关,但您也可以使用 pathinfo() 函数对文件扩展名进行更通用的处理。你不需要硬编码的扩展数组,你可以删除任何扩展。

          【讨论】:

          • 我遇到了这篇文章,因为虽然我有相同的代码 asort() 不起作用,我不知道为什么,我仍然不知道为什么,但我从这里的评论中看到zombat 有同样的问题。感谢您在其中留下评论,这是一个很大的帮助......
          猜你喜欢
          • 2017-01-25
          • 1970-01-01
          • 2012-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多