<?php
/**
* @brief 文件管理类
* @author peng(pp398948781@gmail.com)
* @date 2014-04-23
* @version 1.0
*/
class FileManage
{
/**
* @创建目录 支持多重目录创建
* @param string $dir 目录路径
* @return 创建成功返回true 遇到错误返回false
*/
public static function mkdir($dir = NULL)
{
if(!is_dir($dir))
{
if(!self::mkdir(dirname($dir)))
{
return false;
}
if(!mkdir($dir))
{
return false;
}
}
return true;
}
/**
* @brief 清空指定目录
* @param string $dir 目录路径
* @param bool $del_self 是否删除自身目录 默认false 不删除
* @return bool 清除成功返回true
*/
public static function clearDir($dir,$del_self = false)
{
if($handle = opendir($dir))
{
while(false !== ($item = readdir($handle)))
{
if($item == '.' || $item == '..')
{
continue;
}
if(is_dir("$dir/$item"))
{
self::clearDir("$dir/$item");
rmdir("$dir/$item");
}
else
{
unlink("$dir/$item");
}
}
}
closedir($handle);
$del_self ? rmdir($dir) : '';
return true;
}
/**
* @brief 读取文件/目录信息
* @param string $file 文件名称
* @return array 文件/目录信息
*/
public static function getFileInfo($file)
{
if(!is_file($file) && !is_dir($file))
return false;
$fileInfo = array();
$fileInfo['name'] = basename($file); //文件名
$fileInfo['type'] = self::getFileType($file); //文件类型
$fileInfo['dir'] = dirname($file); //目录名
$fileInfo['size'] = is_dir($file) ? self::getDirSize($file) : self::getFileSize($file); //文件大小
$fileInfo['ctime'] = filectime($file); //文件inode的修改时间
$fileInfo['atime'] = fileatime($file); //上次访问时间
$fileInfo['mtime'] = filemtime($file); //修改时间
$fileInfo['group'] = filegroup($file); //文件的组
$fileInfo['owner'] = fileowner($file); //文件所有者
$fileInfo['perms'] = fileperms($file); //文件权限
$fileInfo['readable'] = is_readable($file); //是否可读
$fileInfo['writeable'] = is_writeable($file); //是否可写
$fileInfo['executable'] = is_executable($file); //是否可执行
$fileInfo['realpath'] = realpath($file); //绝对路径
return $fileInfo;
}
/**
* @brief 复制文件/目录下的文件(不包含当前文件夹)
* @param string $resource 源文件/目录
* @param string $target 目标文件/目录
* @return bool 复制成功返回true 失败返回false
*/
public static function copy($resource,$target = NULL)
{
if(is_file($resource))
{
return copy($resource,$target);
}
else if(is_dir($resource))
{
is_dir($target) || self::mkdir($target);
$files = scandir($resource);
foreach($files as $file)
{
if($file !== '.' && $file !== '..')
{
self::copy("$resource/$file","$target/$file");
}
}
}
else
{
return false;
}
}
/**
* @brief 移动文件/目录下的文件(不包含当前文件夹)
* @param string $resource 源文件/目录
* @param string $target 目标文件/目录
* @return bool 移动成功返回true 失败返回false
*/
public static function move($resource,$target = NULL)
{
if(is_file($resource))
{
return rename($resource,$target);
}
else if(is_dir($resource))
{
is_dir($target) || self::mkdir($target);
$files = scandir($resource);
foreach($files as $file)
{
if($file !== '.' && $file !== '..')
{
self::move("$resource/$file","$target/$file");
}
}
rmdir($resource);
}
else
{
return false;
}
}
/**
* @brief 删除文件/目录
* @param string $resource 目录/文件
* @return bool 删除成功返回true 否则返回false
*/
public static function unlink($resource)
{
if(is_file($resource))
{
return unlink($resource);
}
else if(is_dir($resource))
{
return self::clearDir($resource,true);
}
}
/**
* @brief 读取文件类型(如果加载了fileinfo库 优先使用库函数)
* @param string $fileName 文件名称
* @return string 文件类型
*/
public static function getFileType($fileName)
{
if(get_extension_funcs('fileinfo'))
{
$type = is_file($fileName) ? self::parseMimeType(self::getMimeType($fileName)) : false;
$type = is_dir($fileName) ? 'dir' : '';
return $type ? $type : self::getFileExt($fileName);
}
else
{
return self::getFileTypeByHead($fileName);
}
}
/**
* @brief 通过解析文件头信息获取文件类型
* @param string $fileName 文件名称
* @return string 文件类型
*/
public static function getFileTypeByHead($fileName)
{
$fileRes = is_file($fileName) ? fopen($fileName,'rb') : '';
if(!$fileRes)
{
return false;
}
$bin = fread($fileRes,15);
fclose($fileRes);
if($bin)
{
foreach(self::getTypeList() as $key => $val)
{
$blen = strlen(pack('H*',$key)); //定义的文件头标记字节数
$tbin = substr($bin,0,intval($blen)); //需要比较的文件头长度
if(strtolower($key) == strtolower(array_shift(unpack('H*',$tbin))))
{
return $val;
}
}
}
return 'unknown';
}
/**
* @brief 文件头与文件类型映射表
* @return array 映射列表
*/
public static function getTypeList()
{
return array(
"FFD8FFE1" => "jpg",
"89504E47" => "png",
"47494638" => "gif",
"49492A00" => "tif",
"424D" => "bmp",
"41433130" => "dwg",
"38425053" => "psd",
"7B5C727466" => "rtf",
"3C3F786D6C" => "xml",
"68746D6C3E" => "html",
"44656C69766572792D646174" => "eml",
"CFAD12FEC5FD746F" => "dbx",
"2142444E" => "pst",
"D0CF11E0" => "xls/doc",
"5374616E64617264204A" => "mdb",
"FF575043" => "wpd",
"252150532D41646F6265" => "eps/ps",
"255044462D312E" => "pdf",
"E3828596" => "pwl",
"504B0304" => "zip",
"52617221" => "rar",
"57415645" => "wav",
"41564920" => "avi",
"2E7261FD" => "ram",
"2E524D46" => "rm",
"000001BA" => "mpg",
"000001B3" => "mpg",
"6D6F6F76" => "mov",
"3026B2758E66CF11" => "asf",
"4D546864" => "mid",
);
}
/**
* @brief 读取文件的mime类型 - 需要 php_fileinfo.dll扩展库
* @param string $fileName 文件名称
* @return string 文件的mime类型
*/
public static function getMimeType($fileName)
{
return is_file($fileName) ? mime_content_type(realpath($fileName)) : false;
}
/**
* @brief mime类型和普通类型互相转换
* @param string $type 类型
* @return 返回转换后的类型
*/
public static function parseMimeType($type)
{
if(empty($type))
{
return false;
}
$mime_type = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/x-php',
'css' => 'text/css',
'js' => 'application/javascript',
'js' => 'text/x-c++',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$type_reverse = array_flip($mime_type);
return in_array($type,array_keys($mime_type)) ? $mime_type[$type] : $type_reverse[$type];
}
/**
* @brief 读取文件扩展名
* @param string $file 文件名
* @return string/bool 读取成功返回扩展名 否则返回false
*/
public static function getFileExt($fileName)
{
$fileInfo = is_file($fileName) ? pathinfo($fileName) : NULL;
return $fileInfo ? strtolower($fileInfo['extension']) : false;
}
/**
* @brief 读取文件大小
* @param string $file 文件名称
* @return int/bool 读取成功返回文件大小 否则返回false
*/
public static function getFileSize($fileName)
{
return is_file($fileName) ? filesize($fileName) : false;
}
/**
* @brief 读取文件夹大小
* @param string $dir 文件夹名称
* @return int 文件夹大小
*/
public static function getDirSize($dir)
{
$dirSize = 0;
if($handle = opendir($dir))
{
while(false !== ($item = readdir($handle)))
{
if($item == '.' || $item == '..')
{
continue;
}
if(is_dir("$dir/$item"))
{
$dirSize += self::getDirSize("$dir/$item");
}
else
{
$dirSize += filesize("$dir/$item");
}
}
}
closedir($handle);
return $dirSize;
}
/**
* @brief 判断是否为空目录
* @param string $dir 目录名称
* @return bool 空目录返回true 非空返回false
*/
public static function isDirEmpty($dir)
{
if($handle = opendir($dir))
{
while(false !== ($item = readdir($handle)))
{
if($item == '.' || $item == '..')
{
continue;
}
else
{
return false;
}
}
}
closedir($handle);
return true;
}
}
PHP文件管理类
最新推荐文章于 2024-05-21 09:57:48 发布