目录遍历的类

| 2 Comments | No TrackBacks

<?
/************************************
    Name: ListDir
    Author: Riyao

//This class is to list all file & directory follow a directory
//Parameter:$strdir ,which directory you want list; $del_url,the manage URL,


*********************************/
class ListDir{

 var $dir;
 var $edit_url="";
 var $del_url="";
 
 var $num = 0;

 function ListDir($strdir,$del_url="",$edit_url="")
 {
  $this->dir = $strdir;
  $this->del_url = $del_url;
  $this->edit_url = $edit_url;
  }
 
 function MakeDir($StrDir)
 {
 //global $num;
 if (is_dir($StrDir)) {
      if ($dh = @opendir($StrDir)) {
          while (($file = readdir($dh)) !== false) {
          
           $newdir = $StrDir.$file;
           if($file == "." || $file == "..")
               {
                continue;
               }
              
               $this->num = $this->num + 1;
               print str_repeat("&nbsp;",$this->num);
              
               if(filetype($newdir)=="dir")
               {
                print "<br/>";
                print "$file  [目录]<br/>\n";
               }
               else
               {
                 
                print "<a href='".$this->del_url."?file=".$newdir."'>EDIT</a> <a href='".$newdir."'>$file</a>  <br/>\n";
    }
               if(is_dir($newdir))
               {
                $this->MakeDir($newdir."/");
                }
               
               $this->num = $this->num - 1;
          }
         print "<br/>";
      closedir($dh);
      }
 }

 }
 
 function ShowDir()
 {
  $this->MakeDir($this->dir);
  }
}

/*
exemple

include_once("./class.dir.php");
$dir = new ListDir("c:/","http://127.0.0.1/index.php");
$dir->ShowDir();

*/
?>

No TrackBacks

TrackBack URL: http://www.wujianrong.com/mt-tb.cgi/1435

2 Comments

read()) {
$tem_curnt=$dir."/".$entry;
if($entry=="." || $entry=="..") continue;
if ( is_dir( $tem_curnt)) {
listfile($tem_curnt);
echo "文件夹 ".$tem_curnt."";

$dir_list++;
}
elseif ( is_file($tem_curnt)) {
echo "文件".$tem_curnt."";
$file_list++;
}
}
$d->close();
}

listfile($dir);
echo "目录数:".$dir_list;
echo"";
echo"文件数:".$file_list;
?>

"absolute path of the direcory",
"struct" => array());

As you can see the path itself is put into the "name" field of the
array while the "struct" field is an array.
For each file the function finds it adds an entry to the next
"struct" field containing the files' name.
For each directory the function finds it adds an entry to the next
"struct" field containing a new array of the described structure.

There are two exceptions:
unaccessible directories have a scalar struct = -1
empty directories have a scalar struct = -2

After the function has recursed the whole directory it returns the array

-- Example ---------------------------------------------------------------------

When I recurse a part of my apache htdocs directorytree which contains
some images the array looks like this (PHP comments not included).
Each subarray and therefore each subdirectory is indicated by leading
whitespace:

name = /usr/local/apache/htdocs/media // this is where we start
struct
name = /usr/local/apache/htdocs/media/.1 // first entry is a dir
struct
name = /usr/local/apache/htdocs/media/.1/.3 // same here, dir again
struct // this dir has content
0 = 40.jpg // 0 is the arrays key
1 = tn_40.jpg // the *.jpg's are the value
2 = 41.jpg
3 = tn_41.jpg
4 = 42.jpg
5 = tn_42.jpg
6 = 43.jpg
7 = tn_43.jpg
8 = 44.jpg
9 = tn_44.jpg
10 = 45.jpg
11 = tn_45.jpg
name = /usr/local/apache/htdocs/media/.1/.4 // next subdirectory
struct = -1 // access denied!
name = /usr/local/apache/htdocs/media/.1/.5 // next subdirectory
struct // content again
0 = 81.jpg
1 = tn_81.jpg
2 = 82.jpg
3 = tn_82.jpg
4 = 83.jpg
5 = tn_83.jpg
6 = 84.jpg
7 = tn_84.jpg
8 = 85.jpg
9 = tn_85.jpg
10 = 86.jpg
11 = tn_86.jpg
12 = 87.jpg
13 = tn_87.jpg
14 = 88.jpg
15 = tn_88.jpg
name = /usr/local/apache/htdocs/media/.2 // next subdirectory
struct
name = /usr/local/apache/htdocs/media/.2/.6 // another subdir
struct = -2 // this one is empty!

-- Disclaimer ------------------------------------------------------------------

This script is provided as-is. I take no responsibility on any misbehavior,
errors, damage or whatever else this script might cause.
As the original script by James Holden did not clearly say under which
license it was published I decided to make it freeware. You can use this
script for any purpose you want, non-commercially and commercially alike,
and you're allowed to change the code to whatever pleases you.
You do not need to republish the resulting script as freeware.

If this license offends the intended license by James Holden please let me
know and I will change it as necessary.

*/

function RecurseDir($directory) {
$thisdir = array("name", "struct");
$thisdir['name'] = $directory;
if ($dir = @opendir($directory)) {
$i = 0;
while ($file = readdir($dir)) {
if (($file != ".")&&($file != "..")) {
$tempDir = $directory."/".$file;
if (is_dir($tempDir)) {
$thisdir['struct'][] = RecurseDir($tempDir,$file);
} else {
$thisdir['struct'][] = $file;
}
$i++;
}
}
if ($i == 0) {
// empty directory
$thisdir['struct'] = -2;
}
} else {
// directory could not be accessed
$thisdir['struct'] = -1;
}
return $thisdir;
}
$directory = './'; //这是我加的。
print('');//这是我加的。
print_r(RecurseDir($directory));//这是我加的。
print('');//这是我加的。
?>

Leave a comment

About this Entry

This page contains a single entry by kevinwu published on November 30, 2006 12:16 AM.

rome的一个小例子,读取rss数据 was the previous entry in this blog.

用PHP实现WEB动态网页静态化 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.