dongshou6788 2014-12-08 12:59
浏览 65
已采纳

如何从文件夹中读取/选择文件名时对A - > Z进行排序

This might be very obvious however I cannot seem to solve it.

I have the following option menu :

<select name="Image2" onChange="showImage(this.value)">
    <option value="" selected="selected"></option>
  <?php 
     $dir = "../somefolder";//your path
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        $files[] = $filename;
  echo "<option value='". $filename . "'>" . $filename . "</option>";  }
    sort($files);
 ?>

</select> 
<div id="image_div"></div> 

<script type="text/javascript">
 function showImage(value)
 {
  var img = "<img src='../somefolder/"+value+"' />";
  document.getElementById('image_div').innerHTML = img;
 }
</script>

Which works fine, but all the files are not sorted a-->z is there a way to do this, I have over 100 files in this folder? Any help Welcome

  • 写回答

1条回答 默认 最新

  • dongnuo2879 2014-12-08 13:02
    关注

    You can do two loops, one to read the filenames, and other to output them.

    Since you have about 100 files, it will not change the response time...

    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
            $files[] = $filename;
    }
    sort($files);
    foreach ($files as $filename){
         echo "<option value='". $filename . "'>" . $filename . "</option>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#不用正则表达式如何全字匹配
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计