PHP中怎么实现一个上传类

发布时间:2021-06-30 15:01:18 作者:Leah
来源:亿速云 阅读:103

PHP中怎么实现一个上传类,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<?php /**  *Fileuploadclass  *@version1.0.0(ThuAug1801:32:39CST2005)  *@authorsanshi  */  classupLoad  {  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:00:18CST2005  *@paramstring$info文件内容  *@paramstring$fileName生成的文件名  *@returnboolean建立成功返回true  *@deprecated  *建立html文件  */  functioncreateHtml($info,$fileName)  {  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:03:09CST2005  *@returnvoid  *@deprecated  *构造函数  */  functiondownLoad()  {}  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:03:55CST2005  *@paramstring$fileField在表单中的字段名  *@paramstring$length限制的长度  *@returnboolean成功返回true  *@deprecated  *功能实现函数  */  functioninit($fileField,$length='')  {  $files=$_FILES[$fileField];  //用户名需要改动,根据自己的实际情况做改动  $userName='sanshi';  $fileName=$files['name'];  $fileType=$files['type'];  $fileTemp=$files['tmp_name'];  $fileSize=empty($length)?($files['size']+10):$length;  $fileError=$files['error'];//这块也许php4中没有  //改为  //if($this->_isType($fileName)||$this->_isBig($length ))  if(!$this->_isType($fileName)||$this->_isBig($length )||$fileError!=0)  {  //print_r($files);  returnfalse;  }else{  $path=$this->_createDir($userName);//取得路径  $createFileName=$userName."_".time();//设置当前文件名  $createFileType=$this->getFileType($fileName);//设置文件类别  return@move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType)?true:false;  }  }   /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:07:43CST2005  *@paramint$length上传限制的大小  *@returnboolean超过返回true  *@deprecated  *判断是否超过预定大小  */  function_isBig($length)  {  $bigest='';  return$big>$bigest?true:false;  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:08:55CST2005  *@paramstring$fileName文件名  *@returnstring$fileType文件后缀  *@deprecated  *取得文件后缀(只取得文件的***一个后缀名)  */  functiongetFileType($fileName)  {  returnend(explode('.',$fileName));  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:10:41CST2005  *@paramstring$fileName文件名  *@paramboolean$method是否检查多个后缀默认false  *@paramint$postFix后缀个数默认为2  *@returnboolean存在返回true  *@deprecated  *检查文件的后缀是否在类别数组中,类别数组自己设置  *如果$method设置为true则检查文件有几个后缀  */  function_isType($fileName,$method='false',$postFix=2)  {  //设置类别数组  $type=array('jpeg',  'gif',  'bmp',  'exe');  $fileName=strtolower($fileName);  $fileTypeArray=explode('.',$fileName);  $fileType=end($fileTypeArray);  //判断是否有一个文件有多个后缀  if($method)  {  if(count($fileTypeArray)>(is_int($postFix)?$postFix:2))  {  returnfalse;  }  }  returnin_array($fileType,$type);  }   /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:17:19CST2005  *@paramstring$userName  *@returnstring$path  *@deprecated  *建立目录目录格式年/月/日/用户名/  *权限为755  */  function_createDir($userName)  {  $root='';  $pathSign=DIRECTORY_SEPARATOR;  $y=date('Y').$pathSign;  $m=date('m').$pathSign;  $d=date('d').$pathSign;  $path=$root.$y.$m.$d.$userName;  $dirArray=explode($pathSign,$path);  $tempDir='';  foreach($dirArrayas$dir)  {  $tempDir.=$dir.$pathSign;  $isFile=file_exists($tempDir);  clearstatcache();  if(!$isFile&&!is_dir($tempDir))  {  @mkdir($tempDir,0755);  }  }  return$path.$pathSign;  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:19:32CST2005  *@param string$dirName目录名  *@return boolean可以操作返回true  *@deprecated  *判断操作是否在上传目录  */  function_isDel($dirName)  {  //注意upLoadDir,一定要与真正使用目录相对应  $upLoadDir='';  $upLoadDir=preg_replace('/\\//','\/',$upLoadDir);  $format="/^{$upLoadDir}/";  returnpreg_match($format,$dirName);  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:25:58CST2005  *@paramstring$fileName文件名  *@returnboolean删除文件成功返回true  *@deprecated  *删除文件  */  functiondelFile($fileName)  {  $cur_dir=dirname(trim($fileName));  if($this->_isDel($cur_dir))  {  return@unlink($fileName)?true:false;  }else{  returnfalse;  }  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:27:43CST2005  *@paramstring$dieName目录名  *@returnboolean删除成功返回true  *@deprecated  *删除目录目录下如果有文件不能删除  */  functiondelDir($dirName)  {  if($this->_isDel($dirName)&&is_dir($dirName))  {  return@rmdir($dirName)?true:false;  }else{  returnfalse;  }  }   }  ?> <?php //使用  /*  include'upLoad.class.php';  $up=newupLoad();  if($up->init("file"))  {  echo'success';  }else{  echo'failure';  }  */  ?>

关于PHP中怎么实现一个上传类问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. php如何实现文件上传类
  2. PHP中怎么封装一个多文件上传类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:Bootstrap进度条怎么弄

下一篇:vue如何实现页面加载时的进度条功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》