zoukankan      html  css  js  c++  java
  • 用php实现遍历目录

      用php实现的遍历目录,只遍历第一层,如果制作在线文件管理器的话很管用,不同目录只加一个超链接就行了,然后给方法传递参数就行了,遍历目录的类如下:

     1 class Ergodic{
     2     public function dir($path){
     3         //遍历目录第一层
     4         $handle=opendir($path);    //打开目录
     5         while (($item=readdir($handle))!==false) {
     6             //循环遍历目录
     7             if($item!='.'&&$item!='..'){
     8                 if (is_file ( $path . "/" . $item )) {
     9                     $arr ['file'] [] = $item;
    10                 }
    11                 if (is_dir ( $path . "/" . $item )) {
    12                     $arr ['dir'] [] = $item;
    13                 }
    14             }
    15         }
    16         closedir($handle);
    17         return $arr;
    18     }
    19 }

    这个类中的dir()方法返回一个数组$arr,这个数组就包括我们需要的所有文件名和目录名了,使用方法也很简单,看一下:

     1 $dir=new Ergodic();
     2 $path="resource";
     3 $arr=$dir->dir($path);
     4 echo "文件列表:<br />";
     5 if($arr['file']){
     6     foreach ($arr['file'] as $key => $value) {
     7         echo ($key+1).'&nbsp;&nbsp;'.$value.'<br />';
     8     }
     9 }
    10 echo "目录列表:<br />";
    11 if($arr['dir']){
    12     foreach ($arr['dir'] as $key => $value) {
    13         echo ($key+1).'&nbsp;&nbsp;'.$value.'<br />';
    14     }
    15 }

    这样就可以打印出我们指定的目录遍历结果了,随便建了几个文件,放到目录resource中,测试图片如下:

  • 相关阅读:
    Linux mysql 远程访问
    Linux下高并发socket最大连接数所受的各种限制
    Linux之gunzip命令
    不停在终端中报log
    FIO测试
    yum是什么?(linux命令)
    ubuntu grub 登录
    百度网盘命令行方式,解决ubuntu16.04百度网盘无法运行的问题
    excel使用经验汇总
    ubuntu 安装 ipfs 经验
  • 原文地址:https://www.cnblogs.com/freeweb/p/4604107.html
Copyright © 2011-2022 走看看