123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- <?php
- class UploadFile extends Think
- {
-
- public $maxSize = -1;
-
- public $supportMulti = true;
-
-
- public $allowExts = array();
-
-
- public $allowTypes = array();
-
- public $thumb = false;
-
- public $imageClassPath = 'ORG.Util.Image';
-
- public $thumbMaxWidth;
-
- public $thumbMaxHeight;
-
- public $thumbPrefix = 'thumb_';
- public $thumbSuffix = '';
-
- public $thumbPath = '';
-
- public $thumbFile = '';
-
- public $thumbRemoveOrigin = false;
-
- public $zipImages = false;
-
- public $autoSub = false;
-
- public $subType = 'hash';
- public $dateFormat = 'Ymd';
- public $hashLevel = 1;
-
- public $savePath = '';
- public $autoCheck = true;
-
- public $uploadReplace = false;
-
-
-
- public $saveRule = '';
-
-
- public $hashType = 'md5_file';
-
- private $error = '';
-
- private $uploadFileInfo ;
-
- public function __construct($maxSize='',$allowExts='',$allowTypes='',$savePath='',$saveRule='')
- {
- if(!empty($maxSize) && is_numeric($maxSize)) {
- $this->maxSize = $maxSize;
- }
- if(!empty($allowExts)) {
- if(is_array($allowExts)) {
- $this->allowExts = array_map('strtolower',$allowExts);
- }else {
- $this->allowExts = explode(',',strtolower($allowExts));
- }
- }
- if(!empty($allowTypes)) {
- if(is_array($allowTypes)) {
- $this->allowTypes = array_map('strtolower',$allowTypes);
- }else {
- $this->allowTypes = explode(',',strtolower($allowTypes));
- }
- }
- if(!empty($saveRule)) {
- $this->saveRule = $saveRule;
- }else{
- $this->saveRule = C('UPLOAD_FILE_RULE');
- }
- $this->savePath = $savePath;
- }
-
- private function save($file)
- {
- $filename = $file['savepath'].$file['savename'];
- if(!$this->uploadReplace && is_file($filename)) {
-
- $this->error = '文件已经存在!'.$filename;
- return false;
- }
-
- if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf')) && false === getimagesize($file['tmp_name'])) {
- $this->error = '非法图像文件';
- return false;
- }
- if(in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png')) && getimagesize($file['tmp_name'])) {
- $tmp_imagesize = @getimagesize($file['tmp_name']);
- list($tmp_width, $tmp_height, $tmp_type) = (array)$tmp_imagesize;
- $tmp_size = $tmp_width * $tmp_height;
- if($tmp_size > 16777216 || $tmp_size < 4 || empty($tmp_type) || strpos($tmp_imagesize['mime'], 'flash') > 0) {
- @unlink($file['tmp_name']);
- $this->error = '非法图像文件';
- return false;
- }
- }
- if(!move_uploaded_file($file['tmp_name'], auto_charset($filename,'utf-8','gbk'))) {
- $this->error = '文件上传保存错误!';
- return false;
- }
- if($this->thumb && in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png'))) {
- $image = getimagesize($filename);
- if(false !== $image) {
-
- $thumbWidth = explode(',',$this->thumbMaxWidth);
- $thumbHeight = explode(',',$this->thumbMaxHeight);
- $thumbPrefix = explode(',',$this->thumbPrefix);
- $thumbSuffix = explode(',',$this->thumbSuffix);
- $thumbFile = explode(',',$this->thumbFile);
- $thumbPath = $this->thumbPath?$this->thumbPath:$file['savepath'];
-
- import($this->imageClassPath);
- $realFilename = $this->autoSub?basename($file['savename']):$file['savename'];
- for($i=0,$len=count($thumbWidth); $i<$len; $i++) {
- $thumbname = $thumbPath.$thumbPrefix[$i].substr($realFilename,0,strrpos($realFilename, '.')).$thumbSuffix[$i].'.'.$file['extension'];
- Image::thumb($filename,$thumbname,'',$thumbWidth[$i],$thumbHeight[$i],true);
- }
- if($this->thumbRemoveOrigin) {
-
- unlink($filename);
- }
- }
- }
- if($this->zipImags) {
-
- }
- return true;
- }
-
- public function upload($savePath ='')
- {
-
- if(empty($savePath))
- $savePath = $this->savePath;
-
- if(!is_dir($savePath)) {
-
- if(is_dir(base64_decode($savePath))) {
- $savePath = base64_decode($savePath);
- }else{
-
- if(!mkdir($savePath)){
- $this->error = '上传目录'.$savePath.'不存在';
- return false;
- }
- }
- }else {
- if(!is_writeable($savePath)) {
- $this->error = '上传目录'.$savePath.'不可写';
- return false;
- }
- }
- $fileInfo = array();
- $isUpload = false;
-
-
- $files = $this->dealFiles($_FILES);
- foreach($files as $key => $file) {
-
- if(!empty($file['name'])) {
-
- $file['key'] = $key;
- $file['extension'] = strtolower($this->getExt($file['name']));
- $file['savepath'] = $savePath;
- $file['savename'] = strtolower($this->getSaveName($file));
-
- if($this->autoCheck) {
- if(!$this->check($file))
- return false;
- }
-
- if(!$this->save($file)) return false;
- if(function_exists($this->hashType)) {
- $fun = $this->hashType;
- $file['hash'] = $fun(auto_charset($file['savepath'].$file['savename'],'utf-8','gbk'));
- }
-
- unset($file['tmp_name'],$file['error']);
- $fileInfo[] = $file;
- $isUpload = true;
- }
- }
- if($isUpload) {
- $this->uploadFileInfo = $fileInfo;
- return true;
- }else {
- $this->error = '没有选择上传文件';
- return false;
- }
- }
-
- public function uploadOne($file,$savePath=''){
-
- if(empty($savePath))
- $savePath = $this->savePath;
-
- if(!is_dir($savePath)) {
-
- if(!mk_dir($savePath)){
- $this->error = '上传目录'.$savePath.'不存在';
- return false;
- }
- }else {
- if(!is_writeable($savePath)) {
- $this->error = '上传目录'.$savePath.'不可写';
- return false;
- }
- }
-
- if(!empty($file['name'])) {
- $fileArray = array();
- if(is_array($file['name'])) {
- $keys = array_keys($file);
- $count = count($file['name']);
- for ($i=0; $i<$count; $i++) {
- foreach ($keys as $key)
- $fileArray[$i][$key] = $file[$key][$i];
- }
- }else{
- $fileArray[] = $file;
- }
- $info = array();
- foreach ($fileArray as $key=>$file){
-
- $file['extension'] = $this->getExt($file['name']);
- $file['savepath'] = $savePath;
- $file['savename'] = $this->getSaveName($file);
-
- if($this->autoCheck) {
- if(!$this->check($file))
- return false;
- }
-
- if(!$this->save($file)) return false;
- if(function_exists($this->hashType)) {
- $fun = $this->hashType;
- $file['hash'] = $fun(auto_charset($file['savepath'].$file['savename'],'utf-8','gbk'));
- }
- unset($file['tmp_name'],$file['error']);
- $info[] = $file;
- }
-
- return $info;
- }else {
- $this->error = '没有选择上传文件';
- return false;
- }
- }
-
- private function dealFiles($files) {
- $fileArray = array();
- $n = 0;
- foreach ($files as $file){
- if(is_array($file['name'])) {
- $keys = array_keys($file);
- $count = count($file['name']);
- for ($i=0; $i<$count; $i++) {
- foreach ($keys as $key)
- $fileArray[$n][$key] = $file[$key][$i];
- $n++;
- }
- }else{
- $fileArray[$n] = $file;
- $n++;
- }
- }
- return $fileArray;
- }
-
- protected function error($errorNo)
- {
- switch($errorNo) {
- case 1:
- $this->error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
- break;
- case 2:
- $this->error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
- break;
- case 3:
- $this->error = '文件只有部分被上传';
- break;
- case 4:
- $this->error = '没有文件被上传';
- break;
- case 6:
- $this->error = '找不到临时文件夹';
- break;
- case 7:
- $this->error = '文件写入失败';
- break;
- default:
- $this->error = '未知上传错误!';
- }
- return ;
- }
-
- private function getSaveName($filename)
- {
- $rule = $this->saveRule;
- if(empty($rule)) {
- $saveName = $filename['name'];
- }else {
- if(function_exists($rule)) {
-
- $saveName = $rule().".".$filename['extension'];
- }else {
-
- $saveName = $rule.".".$filename['extension'];
- }
- }
- if($this->autoSub) {
-
- $filename['savename'] = $saveName;
- $saveName = $this->getSubName($filename).'/'.$saveName;
- }
- return $saveName;
- }
-
- private function getSubName($file)
- {
- switch($this->subType) {
- case 'date':
- $dir = date($this->dateFormat,time());
- break;
- case 'hash':
- default:
- $name = md5($file['savename']);
- $dir = '';
- for($i=0;$i<$this->hashLevel;$i++) {
- $dir .= $name{$i}.'/';
- }
- break;
- }
- if(!is_dir($file['savepath'].$dir)) {
- mk_dir($file['savepath'].$dir);
- }
- return $dir;
- }
-
- private function check($file) {
- if($file['error']!== 0) {
-
-
- $this->error($file['error']);
- return false;
- }
-
-
- if(!$this->checkSize($file['size'])) {
- $this->error = '上传文件大小不符!';
- return false;
- }
-
- if(!$this->checkType($file['type'])) {
- $this->error = '上传文件MIME类型不允许!';
- return false;
- }
-
- if(!$this->checkExt($file['extension'])) {
- $this->error ='上传文件类型不允许';
- return false;
- }
-
- if(!$this->checkUpload($file['tmp_name'])) {
- $this->error = '非法上传文件!';
- return false;
- }
- return true;
- }
-
- private function checkType($type)
- {
- if(!empty($this->allowTypes))
- return in_array(strtolower($type),$this->allowTypes);
- return true;
- }
-
- private function checkExt($ext)
- {
- if(!empty($this->allowExts))
- return in_array(strtolower($ext),$this->allowExts,true);
- return true;
- }
-
- private function checkSize($size)
- {
- return !($size > $this->maxSize) || (-1 == $this->maxSize);
- }
-
- private function checkUpload($filename)
- {
- return is_uploaded_file($filename);
- }
-
- private function getExt($filename)
- {
- $pathinfo = pathinfo($filename);
- return $pathinfo['extension'];
- }
-
- public function getUploadFileInfo()
- {
- return $this->uploadFileInfo;
- }
-
- public function getErrorMsg()
- {
- return $this->error;
- }
- }
- ?>
|