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中,测试图片如下:

  • 相关阅读:
    R语言数据框部分笔记
    R语言数组部分的笔记
    R语言向量部分的笔记
    计算机等级考试二级python 第二章 python的基本语法元素
    计算机二级教程python第一章 程序设计语言
    Linux C实现发邮件功能
    telnet收发邮件
    Linux进(线)程同步各种锁
    About Mutex
    wait()与waitpid()与pthread_join()
  • 原文地址:https://www.cnblogs.com/freeweb/p/4604107.html
Copyright © 2011-2022 走看看