UploadFile.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * 文件上传类
  13. * @category ORG
  14. * @package ORG
  15. * @subpackage Net
  16. * @author liu21st <liu21st@gmail.com>
  17. */
  18. class UploadFile {//类定义开始
  19. private $config = array(
  20. 'maxSize' => -1, // 上传文件的最大值
  21. 'supportMulti' => true, // 是否支持多文件上传
  22. 'allowExts' => array(), // 允许上传的文件后缀 留空不作后缀检查
  23. 'allowTypes' => array(), // 允许上传的文件类型 留空不做检查
  24. 'thumb' => false, // 使用对上传图片进行缩略图处理
  25. 'imageClassPath' => 'ORG.Util.Image', // 图库类包路径
  26. 'thumbMaxWidth' => '',// 缩略图最大宽度
  27. 'thumbMaxHeight' => '',// 缩略图最大高度
  28. 'thumbPrefix' => 'thumb_',// 缩略图前缀
  29. 'thumbSuffix' => '',
  30. 'thumbPath' => '',// 缩略图保存路径
  31. 'thumbFile' => '',// 缩略图文件名
  32. 'thumbExt' => '',// 缩略图扩展名
  33. 'thumbRemoveOrigin' => false,// 是否移除原图
  34. 'thumbType' => 1, // 缩略图生成方式 1 按设置大小截取 0 按原图等比例缩略
  35. 'zipImages' => false,// 压缩图片文件上传
  36. 'autoSub' => false,// 启用子目录保存文件
  37. 'subType' => 'hash',// 子目录创建方式 可以使用hash date custom
  38. 'subDir' => '', // 子目录名称 subType为custom方式后有效
  39. 'dateFormat' => 'Ymd',
  40. 'hashLevel' => 1, // hash的目录层次
  41. 'savePath' => '',// 上传文件保存路径
  42. 'autoCheck' => true, // 是否自动检查附件
  43. 'uploadReplace' => false,// 存在同名是否覆盖
  44. 'saveRule' => 'uniqid',// 上传文件命名规则
  45. 'hashType' => 'md5_file',// 上传文件Hash规则函数名
  46. );
  47. // 错误信息
  48. private $error = '';
  49. // 上传成功的文件信息
  50. private $uploadFileInfo ;
  51. public function __get($name){
  52. if(isset($this->config[$name])) {
  53. return $this->config[$name];
  54. }
  55. return null;
  56. }
  57. public function __set($name,$value){
  58. if(isset($this->config[$name])) {
  59. $this->config[$name] = $value;
  60. }
  61. }
  62. public function __isset($name){
  63. return isset($this->config[$name]);
  64. }
  65. /**
  66. * 架构函数
  67. * @access public
  68. * @param array $config 上传参数
  69. */
  70. public function __construct($config=array()) {
  71. if(is_array($config)) {
  72. $this->config = array_merge($this->config,$config);
  73. }
  74. }
  75. /**
  76. * 上传一个文件
  77. * @access public
  78. * @param mixed $name 数据
  79. * @param string $value 数据表名
  80. * @return string
  81. */
  82. private function save($file) {
  83. $filename = $file['savepath'].$file['savename'];
  84. if(!$this->uploadReplace && is_file($filename)) {
  85. // 不覆盖同名文件
  86. $this->error = '文件已经存在!'.$filename;
  87. return false;
  88. }
  89. // 如果是图像文件 检测文件格式
  90. if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf'))) {
  91. $info = getimagesize($file['tmp_name']);
  92. if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){
  93. $this->error = '非法图像文件';
  94. return false;
  95. }
  96. }
  97. if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) {
  98. $this->error = '文件上传保存错误!';
  99. return false;
  100. }
  101. if($this->thumb && in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png'))) {
  102. $image = getimagesize($filename);
  103. if(false !== $image) {
  104. //是图像文件生成缩略图
  105. $thumbWidth = explode(',',$this->thumbMaxWidth);
  106. $thumbHeight = explode(',',$this->thumbMaxHeight);
  107. $thumbPrefix = explode(',',$this->thumbPrefix);
  108. $thumbSuffix = explode(',',$this->thumbSuffix);
  109. $thumbFile = explode(',',$this->thumbFile);
  110. $thumbPath = $this->thumbPath?$this->thumbPath:dirname($filename).'/';
  111. $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名
  112. // 生成图像缩略图
  113. import($this->imageClassPath);
  114. for($i=0,$len=count($thumbWidth); $i<$len; $i++) {
  115. if(!empty($thumbFile[$i])) {
  116. $thumbname = $thumbFile[$i];
  117. }else{
  118. $prefix = isset($thumbPrefix[$i])?$thumbPrefix[$i]:$thumbPrefix[0];
  119. $suffix = isset($thumbSuffix[$i])?$thumbSuffix[$i]:$thumbSuffix[0];
  120. $thumbname = $prefix.basename($filename,'.'.$file['extension']).$suffix;
  121. }
  122. if(1 == $this->thumbType){
  123. Image::thumb2($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);
  124. }else{
  125. Image::thumb($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);
  126. }
  127. }
  128. if($this->thumbRemoveOrigin) {
  129. // 生成缩略图之后删除原图
  130. unlink($filename);
  131. }
  132. }
  133. }
  134. if($this->zipImags) {
  135. // TODO 对图片压缩包在线解压
  136. }
  137. return true;
  138. }
  139. /**
  140. * 上传所有文件
  141. * @access public
  142. * @param string $savePath 上传文件保存路径
  143. * @return string
  144. */
  145. public function upload($savePath ='') {
  146. //如果不指定保存文件名,则由系统默认
  147. if(empty($savePath))
  148. $savePath = $this->savePath;
  149. // 检查上传目录
  150. if(!is_dir($savePath)) {
  151. // 检查目录是否编码后的
  152. if(is_dir(base64_decode($savePath))) {
  153. $savePath = base64_decode($savePath);
  154. }else{
  155. // 尝试创建目录
  156. if(!mkdir($savePath)){
  157. $this->error = '上传目录'.$savePath.'不存在';
  158. return false;
  159. }
  160. }
  161. }else {
  162. if(!is_writeable($savePath)) {
  163. $this->error = '上传目录'.$savePath.'不可写';
  164. return false;
  165. }
  166. }
  167. $fileInfo = array();
  168. $isUpload = false;
  169. // 获取上传的文件信息
  170. // 对$_FILES数组信息处理
  171. $files = $this->dealFiles($_FILES);
  172. foreach($files as $key => $file) {
  173. //过滤无效的上传
  174. if(!empty($file['name'])) {
  175. //登记上传文件的扩展信息
  176. if(!isset($file['key'])) $file['key'] = $key;
  177. $file['extension'] = $this->getExt($file['name']);
  178. $file['savepath'] = $savePath;
  179. $file['savename'] = $this->getSaveName($file);
  180. // 自动检查附件
  181. if($this->autoCheck) {
  182. if(!$this->check($file))
  183. return false;
  184. }
  185. //保存上传文件
  186. if(!$this->save($file)) return false;
  187. if(function_exists($this->hashType)) {
  188. $fun = $this->hashType;
  189. $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
  190. }
  191. //上传成功后保存文件信息,供其他地方调用
  192. unset($file['tmp_name'],$file['error']);
  193. $fileInfo[] = $file;
  194. $isUpload = true;
  195. }
  196. }
  197. if($isUpload) {
  198. $this->uploadFileInfo = $fileInfo;
  199. return true;
  200. }else {
  201. $this->error = '没有选择上传文件';
  202. return false;
  203. }
  204. }
  205. /**
  206. * 上传单个上传字段中的文件 支持多附件
  207. * @access public
  208. * @param array $file 上传文件信息
  209. * @param string $savePath 上传文件保存路径
  210. * @return string
  211. */
  212. public function uploadOne($file,$savePath=''){
  213. //如果不指定保存文件名,则由系统默认
  214. if(empty($savePath))
  215. $savePath = $this->savePath;
  216. // 检查上传目录
  217. if(!is_dir($savePath)) {
  218. // 尝试创建目录
  219. if(!mkdir($savePath,0777,true)){
  220. $this->error = '上传目录'.$savePath.'不存在';
  221. return false;
  222. }
  223. }else {
  224. if(!is_writeable($savePath)) {
  225. $this->error = '上传目录'.$savePath.'不可写';
  226. return false;
  227. }
  228. }
  229. //过滤无效的上传
  230. if(!empty($file['name'])) {
  231. $fileArray = array();
  232. if(is_array($file['name'])) {
  233. $keys = array_keys($file);
  234. $count = count($file['name']);
  235. for ($i=0; $i<$count; $i++) {
  236. foreach ($keys as $key)
  237. $fileArray[$i][$key] = $file[$key][$i];
  238. }
  239. }else{
  240. $fileArray[] = $file;
  241. }
  242. $info = array();
  243. foreach ($fileArray as $key=>$file){
  244. //登记上传文件的扩展信息
  245. $file['extension'] = $this->getExt($file['name']);
  246. $file['savepath'] = $savePath;
  247. $file['savename'] = $this->getSaveName($file);
  248. // 自动检查附件
  249. if($this->autoCheck) {
  250. if(!$this->check($file))
  251. return false;
  252. }
  253. //保存上传文件
  254. if(!$this->save($file)) return false;
  255. if(function_exists($this->hashType)) {
  256. $fun = $this->hashType;
  257. $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
  258. }
  259. unset($file['tmp_name'],$file['error']);
  260. $info[] = $file;
  261. }
  262. // 返回上传的文件信息
  263. return $info;
  264. }else {
  265. $this->error = '没有选择上传文件';
  266. return false;
  267. }
  268. }
  269. /**
  270. * 转换上传文件数组变量为正确的方式
  271. * @access private
  272. * @param array $files 上传的文件变量
  273. * @return array
  274. */
  275. private function dealFiles($files) {
  276. $fileArray = array();
  277. $n = 0;
  278. foreach ($files as $key=>$file){
  279. if(is_array($file['name'])) {
  280. $keys = array_keys($file);
  281. $count = count($file['name']);
  282. for ($i=0; $i<$count; $i++) {
  283. $fileArray[$n]['key'] = $key;
  284. foreach ($keys as $_key){
  285. $fileArray[$n][$_key] = $file[$_key][$i];
  286. }
  287. $n++;
  288. }
  289. }else{
  290. $fileArray[$key] = $file;
  291. }
  292. }
  293. return $fileArray;
  294. }
  295. /**
  296. * 获取错误代码信息
  297. * @access public
  298. * @param string $errorNo 错误号码
  299. * @return void
  300. */
  301. protected function error($errorNo) {
  302. switch($errorNo) {
  303. case 1:
  304. $this->error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
  305. break;
  306. case 2:
  307. $this->error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
  308. break;
  309. case 3:
  310. $this->error = '文件只有部分被上传';
  311. break;
  312. case 4:
  313. $this->error = '没有文件被上传';
  314. break;
  315. case 6:
  316. $this->error = '找不到临时文件夹';
  317. break;
  318. case 7:
  319. $this->error = '文件写入失败';
  320. break;
  321. default:
  322. $this->error = '未知上传错误!';
  323. }
  324. return ;
  325. }
  326. /**
  327. * 根据上传文件命名规则取得保存文件名
  328. * @access private
  329. * @param string $filename 数据
  330. * @return string
  331. */
  332. private function getSaveName($filename) {
  333. $rule = $this->saveRule;
  334. if(empty($rule)) {//没有定义命名规则,则保持文件名不变
  335. $saveName = $filename['name'];
  336. }else {
  337. if(function_exists($rule)) {
  338. //使用函数生成一个唯一文件标识号
  339. $saveName = $rule().".".$filename['extension'];
  340. }else {
  341. //使用给定的文件名作为标识号
  342. $saveName = $rule.".".$filename['extension'];
  343. }
  344. }
  345. if($this->autoSub) {
  346. // 使用子目录保存文件
  347. $filename['savename'] = $saveName;
  348. $saveName = $this->getSubName($filename).$saveName;
  349. }
  350. return $saveName;
  351. }
  352. /**
  353. * 获取子目录的名称
  354. * @access private
  355. * @param array $file 上传的文件信息
  356. * @return string
  357. */
  358. private function getSubName($file) {
  359. switch($this->subType) {
  360. case 'custom':
  361. $dir = $this->subDir;
  362. break;
  363. case 'date':
  364. $dir = date($this->dateFormat,time()).'/';
  365. break;
  366. case 'hash':
  367. default:
  368. $name = md5($file['savename']);
  369. $dir = '';
  370. for($i=0;$i<$this->hashLevel;$i++) {
  371. $dir .= $name{$i}.'/';
  372. }
  373. break;
  374. }
  375. if(!is_dir($file['savepath'].$dir)) {
  376. mkdir($file['savepath'].$dir,0777,true);
  377. }
  378. return $dir;
  379. }
  380. /**
  381. * 检查上传的文件
  382. * @access private
  383. * @param array $file 文件信息
  384. * @return boolean
  385. */
  386. private function check($file) {
  387. if($file['error']!== 0) {
  388. //文件上传失败
  389. //捕获错误代码
  390. $this->error($file['error']);
  391. return false;
  392. }
  393. //文件上传成功,进行自定义规则检查
  394. //检查文件大小
  395. if(!$this->checkSize($file['size'])) {
  396. $this->error = '上传文件大小不符!';
  397. return false;
  398. }
  399. //检查文件Mime类型
  400. if(!$this->checkType($file['type'])) {
  401. $this->error = '上传文件MIME类型不允许!';
  402. return false;
  403. }
  404. //检查文件类型
  405. if(!$this->checkExt($file['extension'])) {
  406. $this->error ='上传文件类型不允许';
  407. return false;
  408. }
  409. //检查是否合法上传
  410. if(!$this->checkUpload($file['tmp_name'])) {
  411. $this->error = '非法上传文件!';
  412. return false;
  413. }
  414. return true;
  415. }
  416. // 自动转换字符集 支持数组转换
  417. private function autoCharset($fContents, $from='gbk', $to='utf-8') {
  418. $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
  419. $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
  420. if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
  421. //如果编码相同或者非字符串标量则不转换
  422. return $fContents;
  423. }
  424. if (function_exists('mb_convert_encoding')) {
  425. return mb_convert_encoding($fContents, $to, $from);
  426. } elseif (function_exists('iconv')) {
  427. return iconv($from, $to, $fContents);
  428. } else {
  429. return $fContents;
  430. }
  431. }
  432. /**
  433. * 检查上传的文件类型是否合法
  434. * @access private
  435. * @param string $type 数据
  436. * @return boolean
  437. */
  438. private function checkType($type) {
  439. if(!empty($this->allowTypes))
  440. return in_array(strtolower($type),$this->allowTypes);
  441. return true;
  442. }
  443. /**
  444. * 检查上传的文件后缀是否合法
  445. * @access private
  446. * @param string $ext 后缀名
  447. * @return boolean
  448. */
  449. private function checkExt($ext) {
  450. if(!empty($this->allowExts))
  451. return in_array(strtolower($ext),$this->allowExts,true);
  452. return true;
  453. }
  454. /**
  455. * 检查文件大小是否合法
  456. * @access private
  457. * @param integer $size 数据
  458. * @return boolean
  459. */
  460. private function checkSize($size) {
  461. return !($size > $this->maxSize) || (-1 == $this->maxSize);
  462. }
  463. /**
  464. * 检查文件是否非法提交
  465. * @access private
  466. * @param string $filename 文件名
  467. * @return boolean
  468. */
  469. private function checkUpload($filename) {
  470. return is_uploaded_file($filename);
  471. }
  472. /**
  473. * 取得上传文件的后缀
  474. * @access private
  475. * @param string $filename 文件名
  476. * @return boolean
  477. */
  478. private function getExt($filename) {
  479. $pathinfo = pathinfo($filename);
  480. return $pathinfo['extension'];
  481. }
  482. /**
  483. * 取得上传文件的信息
  484. * @access public
  485. * @return array
  486. */
  487. public function getUploadFileInfo() {
  488. return $this->uploadFileInfo;
  489. }
  490. /**
  491. * 取得最后一次错误信息
  492. * @access public
  493. * @return string
  494. */
  495. public function getErrorMsg() {
  496. return $this->error;
  497. }
  498. }