- <?php
- /**
- * 文件上傳類
- * @author lijiamin
- * @time 2017-02-17
- * @email 1195989301@qq.com
- */
- classUpload{
- private$allowExt=array('gif','jpg','jpeg','bmp','png','swf');//限制文件上傳的后綴名
- private$maxSize= 1;//限制最大文件上傳1M
- /**
- * 獲取文件的信息
- * @param str $flag 上傳文件的標識
- * @return arr 上傳文件的信息數組
- */
- publicfunctiongetInfo($flag){
- return$_FILES[$flag];
- }
- /**
- * 獲取文件的后綴
- * @param str $filename 文件名
- * @return str 文件擴展名
- */
- publicfunctiongetExt($filename){
- returnpathinfo($filename,PATHINFO_EXTENSION);
- }
- /**
- * 檢測上傳文件是否合法
- * @param str $filename 文件名
- * @return bool 文件擴展名是否合法
- */
- privatefunctioncheckExt($filename){
- $ext=$this->getExt($filename);
- returnin_array($ext,$this->allowExt);
- }
- /**
- * 檢測文件大小是否超過限制
- * @param int size 文件大小
- * @return bool 文件大小是否超過限制
- */
- publicfunctioncheckSize($size){
- return$size<$this->maxSize * 1024 * 1024;<!--$this--->
- }
- /**
- * 隨機的文件名
- * @param int $len 隨機文件名的長度
- * @return str 隨機字符串
- */
- publicfunctionrandName($len=6){
- returnsubstr(str_shuffle('abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ234565789'),0,$len);
- }
- /**
- * 創建文件上傳到的路徑
- * @return str 文件上傳的路徑
- */
- publicfunctioncreateDir(){
- //上傳文件路徑
- $dir='./upload/'.date('Y/m/d',time());
- //判斷文件夾是否存在,不存在則新建
- if(is_dir($dir) ||mkdir($dir,0777,true)){
- return$dir;
- }
- }
- /**
- * 文件上傳
- * @param str $flag 文件上傳標識
- * @return array 返回上傳文件名、保存路徑
- */
- publicfunctionuploadFile($flag){
- if($_FILES[$flag]['name'] ===''||$_FILES[$flag]['error'] !== 0){
- echo"沒有上傳文件";
- return;
- }
- $info=$this->getInfo($flag);
- if(!$this->checkExt($info['name'])){
- echo"不支持的文件類型";
- return;
- }
- if(!$this->checkSize($info['size'])){
- echo"文件大小超過限制";
- return;
- }
- $filename=$this->randName().'.'.$this->getExt($info['name']);
- $dir=$this->createDir();
- if(!move_uploaded_file($info['tmp_name'],$dir.'/'.$filename)){
- echo"文件上傳失敗";
- }else{
- returnarray('filename'=>$filename,'dir'=>$dir);
- }
- }
- }
- ?>
新聞熱點
疑難解答