Files
wnmj-normal/GonghuiWeb/class/files.php
2026-03-03 13:56:44 +08:00

384 lines
10 KiB
PHP

<?php
class files{
function encode($data){
if($data!=null){
$data=rawurlencode($data);
}else{
return false;
}
return $data;
}
function decode($data){
if($data!=null){
$data=rawurldecode($data);
return $data;
} else {
return false;
}
}
function opendir($path,$light=''){
if(preg_match('/[\/]$/',$path)==false){
$path=$path.'/';
}
if(!is_dir($path)){
return false;
}
if(($handle=opendir($path))==false){
return false;
}
while($f=readdir($handle)){
$f=$path.$f;
$fname=$this->typename($f);
if(is_array($light)){
foreach($light as $value){
if($fname==$this->path2name($value)) $fname="<font color=red>".$fname."</font>";
}
}elseif($light){
if($fname==$this->path2name($light)) $fname="<font color=red>".$fname."</font>";
}
if($f==($path.'.')){
$data['.']=realpath($f);
}elseif($f==($path.'..')){
$data['..']=realpath($f);
}elseif(is_dir($f)){
$data['dir'][]=array(
'path'=>realpath($f),
'name'=>$fname,
'power'=>$this->power($f),
'ftime'=>$this->ftime($f),
'etime'=>$this->etime($f)
);
}elseif(is_file($f)){
$data['file'][]=array(
'path'=>realpath($f),
'name'=>$fname,
'size'=>$this->filesize($f),
'typeimg'=>$this->typeimg($f),
'power'=>$this->power($f),
'ftime'=>$this->ftime($f),
'etime'=>$this->etime($f)
);
}else{
$data['other'][]=realpath($f);
}
}
return $data;
}
function page($array,$pagesize=0,$linkNum=7,$page=1){//分页
if(!preg_match('/^[0-9]+$/i',$page) || !$page) $page=1;
$content=array();
$total=count($array);
if(!preg_match('/^[0-9]+$/i',$pagesize) || !$pagesize) $pagesize=$total;
if(!$pagesize) $pagesize=30;
$maxPage=ceil($total/$pagesize);//最大页码
$page=($page>($maxPage)?($maxPage):$page);
$start=($page-1)*$pagesize;
if($pagesize>$total) $pagesize=$total;
for($i=$start;$i<($start+$pagesize);$i++){
if($array[$i]) array_push($content,$array[$i]);
}
$this->start=($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : "1";
$this->end=($page+round($linkNum/2))<$maxPage ? ($page+round($linkNum/2)) : $maxPage;
$this->maxPage=$maxPage;//最大页码
$this->pageSize=$pageSize;//每页显示
$this->linkNum=$linkNum;//链接显示数
$this->totalNum=$total;//总数
$this->page=$page;//当前页
return $content;
}
function getpagecs(){ //获取分页参数
$myarray = array('Page'=>$this->page,'Start'=>$this->start,'End'=>$this->end,'maxPage'=>$this->maxPage,'pageSize'=>$this->pageSize,'linkNum'=>$this->linkNum,'totalNum'=>$this->totalNum);
return $myarray;
}
function power($path){//获取权限
$p=$this->decode($path);
if(preg_match('/%/',$p)){
return '未知';
}else{
return substr(sprintf('%o',fileperms($p)),-4);
}
}
function ftime($path){//获取最后访问时间
$p=$this->decode($path);
if(preg_match('/%/',$p)){
return '未知';
}else{
return $this->times(gmdate("Y-m-d H:i:s",fileatime($p)+8*3600));
}
}
function etime($path){//获取最后修改时间
$p=$this->decode($path);
if(preg_match('/%/',$p)){
return '未知';
}else{
return $this->times(gmdate("Y-m-d H:i:s",filemtime($p)+8*3600));
}
}
function times($time){
if(strtotime($time)>strtotime("today")){
return '<font color=red>今天</font> '.date("m-d H:i:s",strtotime($time));
}elseif(strtotime($time)>strtotime("-2 day")){
return '<font color=#ff6600>昨天</font> '.date("m-d H:i:s",strtotime($time));
}elseif(strtotime($time)>strtotime("last week")){
return '<font color=green>上周</font> '.date("m-d H:i:s",strtotime($time));
}else{
return $time;
}
}
function path2name($path){
return preg_replace('/^.+[\\\\\\/]/', '', $path);
//return basename($path);
}
function typename($path){//获取名称
$c=strrchr($path,'/');
if($c){
return str_replace("/", "", $c);
}
}
function typeimg($path){//获取文件图标
$c=strrchr($path,'.');
if($c){
$c=strtolower($c);//转换小写
if($c=='.txt'){
return "txt";
}elseif($c=='.rar' || $c=='.zip'){
return "rar";
}elseif($c=='.js'){
return "js";
}elseif($c=='.gif' || $c=='.jpg' || $c=='.png' || $c=='.bmp' || $c=='.zip'){
return "jpg";
}elseif($c=='.css'){
return "css";
}elseif($c=='.php'){
return "php";
}elseif($c=='.tpl'){
return "tpl";
}elseif($c=='.asp'){
return "asp";
}elseif($c=='.mp3' || $c=='.wmv'){
return "mp3";
}else{
return "da";
}
}else{
return 'da';
}
}
function filesize($path){
$size=filesize($path);
if($size<=1024) {
return $size.'B';
}elseif($size<=(1024*1024)){
$size=$size/1024;
return number_format($size,3).'KB';
}elseif($size<=(1024*1024*1024)){
$size=$size/1024/1024;
return number_format($size,3).'MB';
}else{
$size=$size/1024/1024/1024;
return number_format($size,3).'GB';
}
}
function adddir($name,$path){//创建目录
if(preg_match('/^([a-zA-Z0-9_\-\.\x{4e00}-\x{9fa5}]+)$/u',$name)!=false){
$paths=iconv("utf-8","gbk",$this->decode($path).'/'.$name);
if(!is_dir($paths)){
mkdir($paths,0777);
}else{
$name=$name."_".mt_rand(1,9999999);
mkdir($this->decode($path).'/'.$name,0777);
}
return $name;
}
}
function re_name($name,$path){//重命名
if(preg_match('/^([a-zA-Z0-9_\-\.\x{4e00}-\x{9fa5}]+)$/u',$name)!=false) {
$path=iconv("utf-8","gbk",$this->decode($path));
$name=iconv("utf-8","gbk",$name);
if(is_dir($path)){
chdir(dirname($path.'/'.$name).'/../');
$to=getcwd().'/'.$name;
if(is_dir($to)){
$to=getcwd().'/'.$name."_".mt_rand(1,9999999);
}
}else{
$to=dirname($path).'/'.$name;
if(file_exists($to)){
$to=dirname($path).'/'.$name.".".mt_rand(1,9999999);
}
}
if(rename($path,$to)!=false){
return $this->decode(dirname($to));
}
}
}
function copyDir($source,$destination){
$result = true;
if(is_dir($source)){
if(!is_dir($destination)){
mkdir($destination,0777);
}else{
$destination=$destination."_".mt_rand(1,9999999);
mkdir($destination,0777);
}
$handle = opendir($source);
while(($file = readdir($handle)) !== false){
if($file != '.' && $file != '..'){
$src = $source . DIRECTORY_SEPARATOR . $file;
$dtn = $destination . DIRECTORY_SEPARATOR . $file;
if(is_dir($src)){
$this->copyDir($src, $dtn);
}else{
if(!copy($src, $dtn)){
$result = false;
break;
}
}
}
}
closedir($handle);
}
return $result;
}
function filemime($path){
if(function_exists('finfo_open')==true){
$finfo=finfo_open(FILEINFO_MIME);
$mime=finfo_file($finfo,$path);
finfo_close($finfo);
return $mime;
}elseif(function_exists('mime_content_type')==true){
return mime_content_type($path);
}else{
return 'false';
}
}
function my_judge_empty_dir($directory){//判断目录是否为空
$handle = opendir($directory);
while(($file = readdir($handle)) !== false){
if ($file != "." && $file != ".."){
closedir($handle);
return false;
}
}
closedir($handle);
return true;
}
function removeDir($dirName){//删除目录
if(!is_dir($dirName)) {
return false;
}
$handle=opendir($dirName);
while(($file=readdir($handle))!==false){
if($file!="."&&$file!=".."){
$dir=$dirName."/".$file;
is_dir($dir) ? $this->removeDir($dir) : unlink($dir);
}
}
closedir($handle);
return rmdir($dirName) ;
}
function removeFile($path){//删除文件
if(file_exists($path)==true){
unlink($path);
}
if(file_exists($path)==false){
return true;
}else{
return false;
}
}
function is_utf8($word){//判断是否UTF8编码文件
if(preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true){
return 'utf-8';
}else{
return 'gb2312';
}
}
function save($path,$content,$charset=''){//保存文件
if($charset=='utf-8'){
$content=iconv("gbk","utf-8",$content);
}
$f=fopen($path,"wb");
fputs($f, $content);
fclose($f);
return true;
}
// function view($path,$size){
// if(filesize($path)==0){
// return false;
// }
// if(($data=file_get_contents($path))==false){
// return false;
// }
// if(function_exists('iconv_substr')==true){
// $total_page=iconv_strlen($data,'utf-8')/$size;
// if($total_page<=1) {
// $total_page=1;
// }
// $i=1;
// while($i<($total_page+1)){
// $idata[]=iconv_substr($data,$i*$size-$size,$size,'utf-8');
// $i++;
// }
// return $idata;
// }else{
// return $data;
// }
// }
function readata($path){//读取文件
if(($data=file_get_contents($path))==false){
return $data=null;
}else{
return $data;
}
}
function urlupload($path,$furl){//上传远程文件
if(function_exists('curl_init')){
$ch=curl_init();
if(($fp=fopen($path,'w'))==false){
return false;
}
curl_setopt($ch,CURLOPT_URL,$furl);
curl_setopt($ch,CURLOPT_FILE,$fp);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return true;
}else{
$fp=fopen($path,'w');
if(!$fp) return false;
$fp2=fopen($furl,'r');
if(!$fp2) return false;
while(!feof($fp2)) {
fwrite($fp,fread($fp2,8192));
}
fclose($fp2);
fclose($fp);
return true;
}
}
function is_url($str){
return preg_match ('/^(http|ftp|https|ftps):\/\/([a-z0-9\-_]+\.)+[a-z]{2,5}/i',$str);
}
function setting($url,$p){
if($data=$this->readata($url)){
$data=str_replace(array(' ','$','<?php','?>',"CONFIG['".strtoupper($p)."']=array("),'',$data);
$data=explode(chr(10),$data);
foreach($data as $values){
if(preg_match("/define+/",$values)){
$datb="1|".str_replace(array('//',',','define','(',')',';','"','\''),array('|','|'),$values);
if($this->is_utf8($datb)=='gb2312') $datb=$datb;
$info[]=explode('|',$datb);
}elseif(preg_match("/=>+/",$values)){
$datb="0|".str_replace(array('//','=>','$',';',',','"','\''),array('|','|'),$values);
if($this->is_utf8($datb)=='gb2312') $datb=$datb;
$info[]=explode('|',$datb);
}
}
return $info;
}
}
}
?>