<?php
class qyFile {
//构造函数
public function __construct() {
return true;
}
//建立文件夹 uploads/20121029/pictrue
public function mkDir($aimUrl) {
//去除多余的/
$aimUrl = str_replace("\\", '/', $aimUrl);
$aimDir = "";
$array = explode("/", $aimUrl);
foreach($array as $v) {
if($v != "") {
$aimDir .= $v.'/';
if(!$this->isExists($aimDir)){
mkdir($aimDir);
}
}
}
}
//建立文件uploads/20121029/pictrue/1.php
public function touch($aimUrl, $overWrite = false) {
if($this->isExists($aimUrl) && $overWrite == false) {
return false;
}elseif($this->isExists($aimUrl) && $overWrite == true) {
}
//获取目录
$dir = dirname($aimUrl);
if(!$this->isExists($dir)) {
$this->mkDir($dir);
}
//创建文件
touch($aimUrl);
return true;
}
//移动文件或文件夹
public function mv($filePath, $aimPath, $overWrite = false) {
if($this->isdir($filePath)) {
$this->mvDir($filePath, $aimPath, $overWrite);
}else{
$this->mvFile($filePath, $aimPath, $overWrite);
}
}
//复制文件或文件夹
public function cp($filePath, $aimPath, $overWrite = false) {
if($this->isDir($filePath)) {
$this->cpDir($filePath, $aimPath, $overWrite);
}else{
$this->cpFile($filePath, $aimPath, $overWrite);
}
}
//删除文件或文件夹
public function rm($filePath) {
if($this->isDir($filePath)) {
$this->rmDir($filePath);
}else{
$this->rmFile($filePath);
}
}
//统计文件或文件夹的大小
public function size($file) {
if($this->isDir($file)) {
return $this->SizeCount($this->dirSize($file));
}else{
return $this->SizeCount($this->Fsize($file));
}
}
//统计目录大小
private function dirSize($dirname) {
//判断目录是否存在
if(!$this->isExists($dirname) || !is_dir($dirname)) {
return false;
}
//打开目录
$dirHandle = @opendir($dirname);
if(!$dirHandle) {
return false;
}
$dirSize = 0;
while($file = readdir($dirHandle)) {
if($file == "." || $file == "..") {
continue;
}
$path = $dirname.DIRECTORY_SEPARATOR.$file;
if(is_dir($path)) {
$dirSize += $this->dirSize($path);
}else {
$dirSize += $this->Fsize($path);
}
}
closedir($dirHandle);
return $dirSize;
}
//统计文件大小
private function Fsize($file) {
//判断是否存在,并且是文件
if(!$this->isExists($file) || !is_file($file)){
return false;
}
return filesize($file);
}
//文件大小单位的计算
private function SizeCount($fileSize) {
$suffix = "";
if($fileSize > pow(2,40)) {
$return = round($fileSize/pow(2,40), 2);
$suffix = "TB";
}else if($fileSize > pow(2,30)) {
$return = round($fileSize/pow(2,30), 2);
$suffix = "GB";
}else if($fileSize > pow(2,20)) {
$return = round($fileSize/pow(2,20), 2);
$suffix = "MB";
}else if($fileSize > pow(2,10)) {
$return = round($fileSize/pow(2,10), 2);
$suffix = "KB";
}else{
$return = $fileSize;
$suffix = "bytes";
}
return $return.$suffix;
}
//将数据写入文件或追加到文件末尾
public function write($file, $content, $append = false) {
if(!$this->isExists(dirname($file))) {
$this->mkDir(dirname($file));
}
$type = $append ? "ab" : "wb";
if(!$fp = @fopen($file,$type)) {
return false;
}
@flock($fp, LOCK_EX);
$ok = @fwrite($fp,$content);
@flock($fp, LOCK_UN);
@fclose($fp);
return $ok;
}
//读取一行
public function readLine($file, $size = 4096){
static $fileArr = array();
if(!$fileArr[$file]) {
$fileArr[$file] = @fopen($file, 'rb');
}
$fp = $fileArr[$file];
if($fp && !feof($fp)) {
return fgets($fp, $size);
}
fclose($fp);
unset($fileArr[$file]);
return false;
}
//读取所有数组
public function readAll($file) {
if(!is_file($file)){
return false;
}
return file_get_contents($file);
}
//移动文件夹
private function mvDir($oldPath, $aimPath, $overWrite = false) {
$oldPath = str_replace('\\', '/', $oldPath);
$oldPath = rtrim($oldPath, '/').'/';
$aimPath = str_replace('\\', '/', $aimPath);
$aimPath = rtrim($aimPath, '/').'/';
//判断是否是目录
if(!is_Dir($oldPath)) {
return false;
}
//判断目标路径是否存在
if(!$this->isExists($aimPath)) {
$this->mkDir($aimPath);
}
//打开目录
@$dirHandle = opendir($oldPath);
if(!$dirHandle) {
return false;
}
//遍历目录
while($file = readdir($dirHandle)) {
if($file == '.' || $file == '..') {
continue;
}
if(!is_dir($oldPath.$file)) {
$this->mvFile($oldPath.$file, $aimPath.$file, $overWrite);
}else {
$this->mvDir($oldPath.$file, $aimPath.$file, $overWrite);
}
}
//关闭资源
closedir($dirHandle);
}
//移动文件
private function mvFile($fileUrl, $aimUrl, $overWrite) {
//判断文件是否存在
if(!$this->isExists($fileUrl)) {
return false;
}
if($this->isExists($aimUrl) && $overWrite == false) {
return false;
}elseif($this->isExists($aimUrl) && $overWrite == true){
$this->rmFile($aimUrl);
}
$dir = dirname($aimUrl);
$this->mkDir($dir);
rename($fileUrl, $aimUrl);
return true;
}
//删除文件夹
private function rmDir($aimDir) {
$aimDir = str_replace('\\', '/', $aimDir);
$aimDir = rtrim($aimDir,'/').'/';
//判断是否是目录
if(!is_dir($aimDir)) {
return false;
}
@$dirHandle = opendir($aimDir);
while($file = readdir($dirHandle)) {
if($file == '.' || $file == '..') {
continue;
}
if(!is_dir($aimDir.$file)) {
$this->rmFile($aimDir.$file);
}else{
$this->rmDir($aimDir.$file);
}
}
closedir($dirHandle);
return rmdir($aimDir);
}
//删除文件
private function rmFile($aimFile) {
if($this->isExists($aimFile)) {
unlink($aimFile);
return true;
}else{
return false;
}
}
//判断当前文件是否是一个文件夹
public function isDir($path) {
return @is_dir($path);
}
//判断文件是否存在
public function isExists($path) {
return @file_exists($path);
}
//复制目录
private function cpDir($oldDir, $aimDir, $overWrite) {
$oldDir = str_replace('\\', '/', $oldDir);
$oldDir = rtrim($oldDir, '/').'/';
$aimDir = str_replace('\\', '/', $aimDir);
$aimDir = rtrim($aimDir, '/').'/';
if(!$this->isExists($oldDir)) {
return false;
}
if(!$this->isExists($aimDir)) {
$this->mkDir($aimDir);
}
$dirHandle = opendir($oldDir);
if(!$dirHandle) {
return false;
}
while($file = readdir($dirHandle)) {
if($file == '.' || $file == '..') {
continue;
}
if(!$this->isDir($oldDir.$file)){
$this->cpFile($oldDir.$file, $aimDir.$file, $overWrite);
}else{
$this->cpDir($oldDir.$file, $aimDir.$file, $overWrite);
}
}
return closedir($dirHandle);
}
//复制文件
private function cpFile($fileUrl, $aimUrl, $overWrite) {
//判断文件是否存在
if(!$this->isExists($fileUrl)) {
return false;
}
if($this->isExists($aimUrl) && $overWrite == false) {
return false;
}elseif($this->isExists($aimUrl) && $overWrite == true) {
$this->rmFile($aimUrl);
}
$dir = dirname($aimUrl);
$this->mkDir($dir);
copy($fileUrl,$aimUrl);
return true;
}
}
$file = new qyFile();
//$file->mkDir('uploads/20121029/pictrue');
//$file->touch('uploads/20121029/pictrue/1.php');
//$file->mvDir('uploads','myupload','false');
//$file->rmDir('myupload');
// $file->rmFile('uploads/1.txt');
//$file->mvFile('uploads/1.txt','myupload/1.txt',false);
//$file->mv('uploads','myupload',false);
//$file->cpdir('uploads','up',false);
//$file->write('myupload/1.txt','11111111111', false);
//echo $file->readAll('myupload/1.txt');
//echo $file->SizeCount($file->dirSize('Guest0.1'));
echo $file->size('myupload');
?>