从read()排序图像;目录功能

时间:2013-05-25 14:01:02

标签: php sorting

这是我的代码。但read():在while循环中似乎并没有按字母顺序排序我的结果。有没有办法对结果进行排序? TKS Seby

 <?php
 $save_path_folder_images = '../simplegallery/users/'.$_SESSION['user_id'].'       /'.$_REQUEST['gal_folder'].'/thumbs/';
 $save_path_folder_images_full = '/simplegallery/users/'.$_SESSION['user_id'].'/'.$_REQUEST['gal_folder'].'/slides/';
$folder=dir($save_path_folder_images);

$counter = 1;
while($folderEntry=$folder->read())
{
    if($folderEntry!="." && $folderEntry!="..")
    {?>
         <div class="imgs" >
                <div class="thumb" >                        
                        <label for="img<?php echo $counter; ?>"><img class="img" src="<?php echo $save_path_folder_images.$folderEntry; ?>" /></label>                          
                    </div>
                <div class="inner">
                    <input type="checkbox" class="chk " id="img<?php echo $counter; ?>" name="img_name[]" value="<?php echo $save_path_folder_images_full.$folderEntry; ?>" />
                </div>
            </div>
        <?php
    $counter++;
    }
}
  ?>

2 个答案:

答案 0 :(得分:1)

->read()不会尝试对输出进行排序,您可以遍历read()一次,将输出推送到数组中。然后通过natcasesort()http://php.net/natcasesort)运行它以对数组进行排序,然后将其打印出来。

所以两个循环。

while($folderEntry=$folder->read())
{
  $fileList[]  = $folderEntry;
}
natcasesort($fileList);
foreach($fileList as $folderEntry)
{
  //your printing
}

答案 1 :(得分:0)

为什么不使用DirectoryIterator

$files = array();
$dir = new DirectoryIterator($save_path_folder_images);
foreach ($dir as $fileinfo) {     
   if ($fileinfo->isFile()) {
       $files[] = $fileinfo->getFilename();
   }
}

natcasesort($files);

同时检查此帖子Sorting files with DirectoryIterator