/Examples/Blog/Lib/ORG/UploadFile.class.php
PHP | 629 lines | 332 code | 35 blank | 262 comment | 64 complexity | fd750f831b7a8b891a27a5aa3f1a3582 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // $Id: UploadFile.class.php 2601 2012-01-15 04:59:14Z liu21st $
-
- /**
- +------------------------------------------------------------------------------
- * ?????
- +------------------------------------------------------------------------------
- * @category ORG
- * @package ORG
- * @subpackage Net
- * @author liu21st <liu21st@gmail.com>
- * @version $Id: UploadFile.class.php 2601 2012-01-15 04:59:14Z liu21st $
- +------------------------------------------------------------------------------
- */
- class UploadFile {//?????
-
- // ????????
- 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;
- // ??????? ????hash date
- public $subType = 'hash';
- public $dateFormat = 'Ymd';
- public $hashLevel = 1; // hash?????
- // ????????
- public $savePath = '';
- public $autoCheck = true; // ????????
- // ????????
- public $uploadReplace = false;
-
- // ????????
- // ????? time uniqid com_create_guid ?
- // ??????????????? ?????????
- public $saveRule = '';
-
- // ????Hash?????
- // ????? md5_file sha1_file ?
- public $hashType = 'md5_file';
-
- // ????
- private $error = '';
-
- // ?????????
- private $uploadFileInfo ;
-
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- */
- 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;
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param mixed $name ??
- * @param string $value ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- 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(!move_uploaded_file($file['tmp_name'], $this->autoCharset($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) {
- // TODO ??????????
-
- }
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $savePath ????????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- 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??????
- $files = $this->dealFiles($_FILES);
- foreach($files as $key => $file) {
- //???????
- if(!empty($file['name'])) {
- //???????????
- $file['key'] = $key;
- $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($this->autoCharset($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;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ???????????? ?????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param array $file ??????
- * @param string $savePath ????????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- 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($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
- }
- unset($file['tmp_name'],$file['error']);
- $info[] = $file;
- }
- // ?????????
- return $info;
- }else {
- $this->error = '????????';
- return false;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ????????????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param array $files ???????
- +----------------------------------------------------------
- * @return array
- +----------------------------------------------------------
- */
- 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;
- }
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $errorNo ????
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- 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 ;
- }
-
- /**
- +----------------------------------------------------------
- * ?????????????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param string $filename ??
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- 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;
- }
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param array $file ???????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- 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;
- }
-
- /**
- +----------------------------------------------------------
- * ???????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param array $file ????
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- private function check($file) {
- if($file['error']!== 0) {
- //??????
- //??????
- $this->error($file['error']);
- return false;
- }
- //????????????????
- //??????
- if(!$this->checkSize($file['size'])) {
- $this->error = '?????????';
- return false;
- }
-
- //????Mime??
- 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 autoCharset($fContents, $from='gbk', $to='utf-8') {
- $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
- $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
- if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
- //??????????????????
- return $fContents;
- }
- if (function_exists('mb_convert_encoding')) {
- return mb_convert_encoding($fContents, $to, $from);
- } elseif (function_exists('iconv')) {
- return iconv($from, $to, $fContents);
- } else {
- return $fContents;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ?????????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param string $type ??
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- private function checkType($type) {
- if(!empty($this->allowTypes))
- return in_array(strtolower($type),$this->allowTypes);
- return true;
- }
-
-
- /**
- +----------------------------------------------------------
- * ?????????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param string $ext ???
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- private function checkExt($ext) {
- if(!empty($this->allowExts))
- return in_array(strtolower($ext),$this->allowExts,true);
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ??????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param integer $size ??
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- private function checkSize($size) {
- return !($size > $this->maxSize) || (-1 == $this->maxSize);
- }
-
- /**
- +----------------------------------------------------------
- * ??????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param string $filename ???
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- private function checkUpload($filename) {
- return is_uploaded_file($filename);
- }
-
- /**
- +----------------------------------------------------------
- * ?????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param string $filename ???
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- private function getExt($filename) {
- $pathinfo = pathinfo($filename);
- return $pathinfo['extension'];
- }
-
- /**
- +----------------------------------------------------------
- * ?????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return array
- +----------------------------------------------------------
- */
- public function getUploadFileInfo() {
- return $this->uploadFileInfo;
- }
-
- /**
- +----------------------------------------------------------
- * ??????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- public function getErrorMsg() {
- return $this->error;
- }
-
- }