即兴写的代码,大家可以完善一下
/**
* dirtree.php 递归列出目录
*
* @copyright
* @author skycrack
* @created
* @version $id$
*/
define('_debug', 1);
class dirtree
{
private $_dirroot;
private $_filter;
private $_tmpbuff = array();
public function __construct($dirroot = '.')
{
$this->_dirroot = $dirroot;
}
//使用 过滤器 或者 设置 $_safefile ....
public function setfilter($filter)
{
$this->_filter = $filter;
}
public function listdirfile($dir = '', $action='')
{
$curdir = ( empty($dir) ) ? $this->_dirroot : $dir;
$dh = @opendir($curdir);
while ( $tmpname = readdir($dh) )
{
if ( ($tmpname == '.') || ($tmpname == '..') ) continue;
$totalpath = $curdir . '/' . $tmpname;
if ( is_object($this->_filter) )
{
if ( $this->_filter->dofilter($totalpath) ) continue;
}
if ( is_dir($totalpath) )
{
$this->_tmpbuff['0'][] = $tmpname;
if ( _debug )
{
echo 'is dir:' . $totalpath . '
';
}
if ( is_object($action) )
{
$action->doaction($totalpath);
}
$this->listdirfile($totalpath, $action);
}
else
{
$this->_tmpbuff['1'][] = $tmpname;
if ( _debug )
{
echo 'is file:' . $totalpath . '
';
}
if ( is_object($action) )
{
$action->doaction($totalpath);
}
}
}
closedir($dh);
}
}
interface diraction
{
public function doaction($args);
}
interface dirfilter
{
public function dofilter($args);
}
class nowaction implements diraction
{
public function doaction($args)
{
if ( _debug )
{
$numargs = func_num_args();
echo $numargs . '
';
for( $i = 0; $i print_r(func_get_arg($i) . '
');
}
}
}
=====================================================
应用 部分
set_time_limit(0);
require 'DirTree.php';
class Gbk2Utf8Action implements DirAction
{
public function doAction($args)
{
$aimPath = ereg_replace('D:/html/web','D:/back', $args);
if ( is_file($args) )
{
$file = implode ('', file($args));
$content = iconv("gb2312", "UTF-8", $file);
$fh = fopen($aimPath, 'w');
fwrite($fh, $content);
fclose($fh);
}
else
{
mkdir($aimPath);
}
}
}
class HtmlPhpFilter implements DirFilter
{
public function doFilter($args)
{
$suffix = substr(strrchr($args, '.'), 1);
if ( ('htm' == $suffix) || ('php' == $suffix) )
return false;
else if ( is_dir($args) )
return false;
else
return true;
}
}
$dirTree = new DirTree();
$action = new Gbk2Utf8Action();
$filter = new HtmlPhpFilter();
$dirTree->setFilter($filter);
$dirTree->listDirFile('D:/html/web', $action);










