<?php?namespace common\uploads;/**?* 構(gòu)建上傳文件信息?* @return unknown?*/class Upload{public static function getFiles(){ $i=0;? ? $files = []; foreach($_FILES as $file){ if(is_string($file['name'])){ $files[$i]=$file; $i++; }elseif(is_array($file['name'])){ foreach($file['name'] as $key=>$val){ $files[$i]['name']=$file['name'][$key]; $files[$i]['type']=$file['type'][$key]; $files[$i]['tmp_name']=$file['tmp_name'][$key]; $files[$i]['error']=$file['error'][$key]; $files[$i]['size']=$file['size'][$key]; $i++; } } } return $files; }? ? public static function getExt($filename){ ? return strtolower(pathinfo($filename,PATHINFO_EXTENSION));}/**?* 產(chǎn)生唯一字符串?* @return string?*/public static function getUniName(){ return md5(uniqid(microtime(true),true));}/**?* 針對(duì)于單文件、多個(gè)單文件、多文件的上傳?* @param array $fileInfo?* @param string $path?* @param string $flag?* @param number $maxSize?* @param array $allowExt?* @return string?*/public static function uploadFile($fileInfo,$path='../web/uploads',$flag=true,$maxSize=1048576,$allowExt=array('jpeg','jpg','png','gif')){ //$flag=true; //$allowExt=array('jpeg','jpg','gif','png'); //$maxSize=1048576;//1M //判斷錯(cuò)誤號(hào)? ?? if($fileInfo['error']===UPLOAD_ERR_OK){ //檢測(cè)上傳得到小 if($fileInfo['size']>$maxSize){ $res['mes']=$fileInfo['name'].'上傳文件過大'; } $ext= ?self::getExt($fileInfo['name']); //檢測(cè)上傳文件的文件類型 if(!in_array($ext,$allowExt)){ $res['mes']=$fileInfo['name'].'非法文件類型'; } //檢測(cè)是否是真實(shí)的圖片類型 if($flag){ if(!getimagesize($fileInfo['tmp_name'])){ $res['mes']=$fileInfo['name'].'不是真實(shí)圖片類型'; } } //檢測(cè)文件是否是通過HTTP POST上傳上來的 if(!is_uploaded_file($fileInfo['tmp_name'])){ $res['mes']=$fileInfo['name'].'文件不是通過HTTP POST方式上傳上來的'; }// if($res) return $res; //$path='./uploads'; if(!file_exists($path)){ mkdir($path,0777,true); chmod($path,0777); } $uniName = self::getUniName(); $destination=$path.'/'.$uniName.'.'.$ext; if(!move_uploaded_file($fileInfo['tmp_name'],$destination)){ $res['mes']=$fileInfo['name'].'文件移動(dòng)失敗'; }? ? ? ?? $res['mes']=$fileInfo['name'].'上傳成功'; $res['dest']=$destination; return $res; }else{ //匹配錯(cuò)誤信息 switch ($fileInfo ['error']) { case 1 : $res['mes'] = '上傳文件超過了PHP配置文件中upload_max_filesize選項(xiàng)的值'; break; case 2 : $res['mes'] = '超過了表單MAX_FILE_SIZE限制的大小'; break; case 3 : $res['mes'] = '文件部分被上傳'; break; case 4 : $res['mes'] = '沒有選擇上傳文件'; break; case 6 : $res['mes'] = '沒有找到臨時(shí)目錄'; break; case 7 : case 8 : $res['mes'] = '系統(tǒng)錯(cuò)誤'; break; } return $res; }}}這是源碼不知道要在那里改= =
求實(shí)現(xiàn)圖片上傳后自動(dòng)生成多張不同尺寸的圖片的實(shí)現(xiàn)思路謝謝了
create_time
2015-07-10 13:13:54