<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied'); class file { static function mk_dir($dir, $filename = false){ $adir = explode('/',$dir); if($filename){ array_pop($adir); } $rootdir = array_shift($adir); if(($rootdir != '.'|| $rootdir != '..') && !file_exists($rootdir)){ @mkdir($rootdir); } $dirlist = ''; foreach($adir as $key=>$val){ if($val != '.' && $val != '..'){ $dirlist .= "/".$val; $dirpath = $rootdir.$dirlist; if(!file_exists($dirpath)){ @mkdir($dirpath,0777); } } } return true; } static function read_file($filename){ $content = ''; if(function_exists('file_get_contents')){ @$content = file_get_contents($filename); }else{ if(@$fp = fopen($filename, 'r')){ @$content = fread($fp, filesize($filename)); @fclose($fp); } } return $content; } static function write_file($filename, $content, $openmod='wb'){ if(@$fopen = fopen($filename, $openmod)){ flock($fopen, 2); fwrite($fopen, $content); fclose($fopen); return true; }else{ return false; } } static function del_file($file){ if(file_exists($file) != false){ unlink($file); }else{ die('文件不存在'); } } static function del_dir($dir_name){ if(!file_exists($dir_name)){ return false; } $dir = opendir($dir_name); while($fileName = readdir($dir)){ $file = $dir_name.'/'.$fileName; if($fileName != '.' && $fileName != '..'){ if(is_dir($file)){ self::del_dir($file); }else{ @unlink($file); } } } closedir($dir); return @rmdir($dir_name); } static function copy_dir($surDir, $toDir){ $surDir = rtrim($surDir,'/').'/'; if(!file_exists($surDir)){ return false; } $toDir = rtrim($toDir,'/').'/'; if(!file_exists($toDir)){ self::mk_dir($toDir); } $file = opendir($surDir); while($fileName = readdir($file)){ $file1 = $surDir . $fileName; $file2 = $toDir . $fileName; if($fileName != '.' && $fileName != '..'){ if(is_dir($file1)){ self::copy_dir($file1, $file2); }else{ copy($file1, $file2); } } } closedir($file); return true; } static function filename($file){ return date("y").'/'.date("md").'/'.get_rand(3).(microtime()*1000000).date("his",time()).'.'.self::getExt($file); } static function getExt($path){ $path = pathinfo($path); return strtolower($path['extension']); } static function get_dirs($dir){ $dir = rtrim($dir,'/').'/'; $dirArray[][] = NULL; if (false != ($handle = opendir($dir))){ $i = 0; $j = 0; while(false !== ($file = readdir($handle))) { if(is_dir($dir.$file)){ $dirArray['dir'][$i] = $file; $i++; }else{ $dirArray['file'][$j] = $file; $j++; } } closedir($handle); } return $dirArray; } static function get_file_list($path, $path_line = null){ return self::get_file_list_func($path, $path_line); } static function get_file_list_func($path, $path_line = null){ static $result_arr = array(); if($dh = opendir($path)){ while(($file = readdir($dh)) !== false){ if($file{0} == '.'){ continue; } if($file != '.' && $file != '..'){ $file_aaa = $path.'/'.$file; if($path_line){ $result_arr[] = str_replace($path_line, '', strstr($file_aaa, $path_line)); }else{ $result_arr[] = $file_aaa; } if(is_dir($path."/".$file)){ self::get_file_list_func($path."/".$file, $path_line); } } } closedir($dh); } return $result_arr; } static function get_size($dir){ $dirlist = opendir($dir); $dirsize = 0; while (false !== ($folderorfile = readdir($dirlist))){ if($folderorfile != "." && $folderorfile != ".."){ if (is_dir("$dir/$folderorfile")){ $dirsize += self::get_size("$dir/$folderorfile"); }else{ $dirsize += filesize("$dir/$folderorfile"); } } } closedir($dirlist); return $dirsize; } static function empty_dir($dir){ return (($files = @scandir($dir)) && count($files) <= 2); } }