<?php
// 单位自动转换函数
function getRealSize($size){
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($size < $kb){
return $size." B";
}else if($size < $mb){
return round($size/$kb,2)." KB";
}else if($size < $gb){
return round($size/$mb,2)." MB";
}else if($size < $tb){
return round($size/$gb,2)." GB";
}else{
return round($size/$tb,2)." TB";
}
}
//获取目录下子目录及文件信息
function getDir($path,$root=""){
$realpath = realpath($path);
$realroot = realpath($root);//检查要读取的目录是否在根目录之内
if(strpos($realpath,$realroot)===false){
die("没有权限访问改目录");
}//当前目录就是根目录的时候,不显示“上级目录”的连接
if($path!=$root && $realpath!=$realroot)
$dirarray = array("上级目录"=>dirname($realpath));
$filearray = array();
$dir = new DirectoryIterator($path);
foreach ($dir as $item){
if(!$dir->isDot()){
if($dir->isDir()){
$dirarray[$dir->getFilename()] = urlencode($path."/".$dir->getFilename());
}else{
$filearray[$dir->getFilename()] = array('path'=>urlencode($path."/".$dir->getFilename()),'size'=>getRealSize($dir->getSize()),'atime'=>$dir->getAtime(),'mtime'=>$dir->getMtime());
}
}
}
$array = array('dir'=>$dirarray,'file'=>$filearray);return $array;}//如果未指定路径,则当前路径为根目录
if(!empty($_GET['path']))
$path = urldecode(trim($_GET['path']));
else
$path = ".";//指定根目录 如果允许程序访问当前页面所在目录之外的目录,这里请使用绝对路径,否则请使用相对路径
$root = "e:/www";//获取目录信息
$files = getDir($path,$root);
$smarty->assign("files",$files);
$smarty->display("dir.htm");
?>
本博客文章非特别注明均属原创,如需转载请保留本博客地址:http://dao.daimaku.com
[