类文件:
<?php
/**
* 文件系统管理类 ,简单的熟悉文件操作函数
* 本类将包含下面功能:
* 1.浏览当前目录下的文件
* 2.打开上/下一级目录
* 3.新建/删除文件夹/文件
* 4.统计文件大小
* @author prg 2014/02/28
*/
define('SEP', DIRECTORY_SEPARATOR); //定义分隔符
class MyDirectory
{
//这里定义为扫描windows文件的字符编码,指读取这两种字符
//但是任然不能打开中文文件夹下面的子文件加????
private static $enclist = array('UTF-8', 'GBK');
private $dir_handle;//dir resource
private $path;//当前路径
private $arr_file;//存储当前目录的文件/夹
private $cur_script;//取得当前运行的脚本
private $dir_size = FALSE;//是否统计文件夹大小
function __construct($path)
{
$this->path = $path;
if(file_exists($path))
{
$this->dir_handle = opendir($this->path);
}
$this->cur_script = basename($_SERVER['SCRIPT_FILENAME']);//获取当前脚本名称
}
/**
* 设置是否统计文件夹大小
* @param bool $flag
*/
function setDirsize($flag)
{
if(is_bool($flag))
{
$this->dir_size = $flag;
}
}
/**
* 获取父路径
* @param string $path
* @return string
*/
private function ParentPath($path)
{
$i=strlen($path)-1;
for(;$i>=0;$i--)
{
if($path{$i}==SEP)
{
$path = substr($path, 0,$i);
break;
}
}
return $path;
}
/**
* 扫描当前目录
* @return boolean
*/
private function scanCurDir()
{
if(!is_resource($this->dir_handle))
{
return false;
}
while(($file=readdir($this->dir_handle))!==false)
{
if($file!='.'&&$file!='$RECYCLE.BIN')
{
$mb=mb_detect_encoding($file,self::$enclist);
$file = iconv($mb, 'UTF-8', $file);
if($file!='..')
{
$this->arr_file[$file] = $this->getFileInfo($this->path.SEP.$file);
}
else
{
$this->arr_file[$file] = array(0,0);
}
}
}
return true;
}
/**
* 获取目录结构
* @return Ambigous <multitype:number , NULL, multitype:string, multitype:string Ambigous <string, int> >
*/
function getDirectory()
{
$res = $this->scanCurDir();
if(!$res)
{
return null;
}
foreach ($this->arr_file as $k=>$v)
{
if(is_dir($this->path.SEP.$k))
{
if(strpos('..',$k)!==false)
{
$str= "<a href='".$this->cur_script."?path=".$this->ParentPath($this->path)."&k=".rand(1,100)."'>$k</a>";
}
else
{
$str= "<a href='".$this->cur_script."?path=".$this->path.SEP.$k."&k=".rand(1,100)."'>$k</a>";
}
}
else
{
$str = $k;
}
array_unshift($this->arr_file[$k],$str);
}
return $this->arr_file;
}
/**
* 取得文件的修改时间和大小
* @param string $path
* @return NULL|multitype:string
*/
private function getFileInfo($path)
{
if(!file_exists($path))
{
return array(0,0);
}
clearstatcache();
$filemtime= date("Y-m-d H:i:s",filemtime($path));
$filesize = 0;
//获取的文件类型
$type= filetype($path);
if($type=='file')
{
$filesize = filesize($path);
}
elseif($this->dir_size) //获取问价夹大小
{
$this->getDirSize($path, $filesize);
}
$filesize = $this->changeSize($filesize);
$arr_info = array(
'file_mtime'=>$filemtime,
'file_size'=>$filesize
);
return $arr_info;
}
/**
* 递归,获取文件 文件夹大小
* @param string $dir
* @param int $size
*/
private function getDirSize($dir,&$size)
{
//这里不是一个文件夹直接返回文件大小
if(!is_dir($dir))
{
$size = filesize($dir);
return ;
}
$dir_h = opendir($dir);
if(!$dir_h)
{
$size = 0;
return;
}
while(($file=readdir($dir_h))!==FALSE)
{
if($file!='.'&&$file!='..')
{
if(is_dir($dir.SEP.$file))
{
$this->getDirSize($dir.SEP.$file, $size);
}
else
{
$size+=filesize($dir.SEP.$file);
}
}
}
return;
}
/**
* 转换大小
* @param int $size
* @return string
*/
private function changeSize($size)
{
if($size<1024)
{
$size.='B';
}
elseif($size<1024*1024)
{
$size=ceil($size/1024).'KB';
}
elseif($size<1024*1024*1024)
{
$size=ceil($size/(1024*1024)).'MB';
}
else
{
$size=ceil($size/(1024*1024*1024)).'GB';
}
return $size;
}
public function getIsDirsize()
{
return $this->dir_size;
}
/**
* 创建目录
* @param string $name
* @return boolean
*/
public function createDir($name)
{
$name = $this->path.SEP.$name;
if(file_exists($name))
{
echo '目录已经存在';
return false;
}
return mkdir($name,'777');
}
/**
* 删除目录
* @param string $name
*/
public function deleteDir($name)
{
$name = $this->path.SEP.$name;
if(!file_exists($name))
{
echo '要删除的目录不存在';
return false;
}
return rmdir($name);
}
/**
* 删除文件
* @param string $name
* @return boolean
*/
public function deleteFile($name)
{
$name = $this->path.SEP.$name;
if(!is_file($name)||!file_exists($name))
{
echo '删除的不是文件,或者文件不存在';
return false;
}
return unlink($name);
}
/**
* 重命名文件/文件夹
* @param string $old
* @param string $new
* @return bool
*/
public function renameFIle($old,$new)
{
$old = $this->path.SEP.$old;
if(!file_exists($old))
{
echo '要更改的文件不存在 ';
return false;
}
$tmp = $this->path.SEP.$new;
if(file_exists($new))
{
echo '文件名',$new,'已经存在';
return false;
}
return rename($old, $tmp);
}
}
?>
调用文件:
<?php
header('Content-type:text/html;charset=utf-8');
require_once 'directory.class.php';
@$path = $_GET['path'];
if(!isset($path))
{
$path='D:';
}
$di = new MyDirectory($path);
$di->setDirsize(TRUE);
$arr_list = $di->getDirectory();
$cdir = $di->getIsDirsize()?'Yes':'No';
$h = '<h2>目录结构<small>子目录大小?'.$cdir.'</small><h2>';
echo $h,'<hr><table width="50%">';
echo '<tr><td>文件名</td><td>创建时间</td><td>大小</td></tr>';
foreach ($arr_list as $key=>$val)
{
echo '<tr>';
foreach($val as $v)
{
echo '<td>',$v,'</td>';
}
echo '</tr>';
}
echo '</table>';
//$di->createDir('123')
//mkdir(iconv('UTF-8', 'gbk', 'D:\我的文件夹'));
?>