代码实现
traveldir.class.php
//filename:traveldir.class.php
//author:ResearchWorld
<?php
class TravelDir{
private $path = '/';
private $dir_handle = null;
public function travel($path='/',$is_deep=false){
$this->path = $path;
if ($dir_handle = $this->getDirHandle() ){
$this->dir_handle = $dir_handle;
echo '<table>';
echo '<tr>';
echo '<td>'.'文件名'.'</td>';
echo '<td>'.'文件类型'.'</td>';
echo '</tr>';
while( $file_name = readdir($dir_handle)){
$is_ignore = $file_name === '.' || $file_name === '..' ? true : false;
if(strrchr($path,'/') != strlen($path)-1 ){
$path .= '/';
}
$file_type = @filetype($path.$file_name);
echo '<tr>';
echo '<td>'.$file_name.'</td>';
echo '<td>'.$file_type.'</td>';
echo '</tr>';
if($is_deep && $file_type==='dir' && !$is_ignore){
echo '<tr>';
echo '<td>';
echo "<ul stype='padding:0px 5px'>";
echo "<li style='list-style:none;'>";
$this->travel($path.$file_name,$is_deep);
echo '</li>';
echo '</ul>';
echo '</td>';
echo '</tr>';
}
}
echo '</table>';
}
}
private function getDirHandle(){
$path = $this->path;
if(is_dir($path)){
$dir_handle = @opendir($path);
if(!$dir_handle){
return false;
}
return $dir_handle;//其实这里保留这句就可以了,上面多写了.
}
return false;
}
public function __destruct(){
if(!empty($dir_handle))
closedir($dir_hanle);
}
}
index.php
<?php
header('Content-type: text/html; charset=utf-8');
function __autoload($class_name){
$class_name =strtolower($class_name);
$class_file_name = $class_name.'.class'.'.php';
if(file_exists("./${class_file_name}")){
include_once $class_file_name;
}
}
$traveldir = new TravelDir;
$traveldir->travel('../mysql_save_file',true);
效果展示
目录太复杂的没敢测试