FileUploader.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. defined('InShopNC') or exit('Access Invalid!');
  3. class FileUploader
  4. {
  5. /**
  6. * 文件存储路径
  7. */
  8. private $save_path;
  9. /**
  10. * 允许上传的文件类型
  11. */
  12. private $allow_type=array('gif','jpg','jpeg','bmp','png','swf','tbi');
  13. /**
  14. * 允许的最大文件大小,单位为KB
  15. */
  16. private $max_size = '1024';
  17. /**
  18. * 改变后的图片宽度
  19. */
  20. private $thumb_width = 0;
  21. /**
  22. * 改变后的图片高度
  23. */
  24. private $thumb_height = 0;
  25. /**
  26. * 生成扩缩略图后缀
  27. */
  28. private $thumb_ext = false;
  29. /**
  30. * 允许的图片最大高度,单位为像素
  31. */
  32. private $upload_file;
  33. /**
  34. * 是否删除原图
  35. */
  36. private $ifremove = false;
  37. /**
  38. * 上传文件名
  39. */
  40. public $file_name;
  41. /**
  42. * 上传文件后缀名
  43. */
  44. private $ext;
  45. /**
  46. * 上传文件新后缀名
  47. */
  48. private $new_ext;
  49. /**
  50. * 默认文件存放文件夹
  51. */
  52. private $default_dir = ATTACH_PATH;
  53. /**
  54. * 错误信息
  55. */
  56. public $error = '';
  57. /**
  58. * 生成的缩略图,返回缩略图时用到
  59. */
  60. public $thumb_image;
  61. /**
  62. * 是否立即弹出错误提示
  63. */
  64. private $if_show_error = false;
  65. /**
  66. * 是否只显示最后一条错误
  67. */
  68. private $if_show_error_one = false;
  69. /**
  70. * 文件名前缀
  71. *
  72. * @var string
  73. */
  74. private $fprefix;
  75. /**
  76. * 是否允许填充空白,默认允许
  77. *
  78. * @var unknown_type
  79. */
  80. private $filling = true;
  81. private $config;
  82. /**
  83. * 初始化
  84. *
  85. * $upload = new UploadFile();
  86. * $upload->set('default_dir','upload');
  87. * $upload->set('max_size',1024);
  88. * //生成4张缩略图,宽高依次如下
  89. * $thumb_width = '300,600,800,100';
  90. * $thumb_height = '300,600,800,100';
  91. * $upload->set('thumb_width', $thumb_width);
  92. * $upload->set('thumb_height',$thumb_height);
  93. * //4张缩略图名称扩展依次如下
  94. * $upload->set('thumb_ext', '_small,_mid,_max,_tiny');
  95. * //生成新图的扩展名为.jpg
  96. * $upload->set('new_ext','jpg');
  97. * //开始上传
  98. * $result = $upload->upfile('file');
  99. * if (!$result){
  100. * echo '上传成功';
  101. * }
  102. *
  103. */
  104. public function __construct(){
  105. $this->config['thumb_type'] = C('thumb.cut_type');
  106. Language::read('core_lang_index');
  107. }
  108. public function init_files($img,$member_id,&$err,$name_code='member')
  109. {
  110. if(file_exists($img) == false) return false;
  111. $_FILES = [];
  112. $_FILES['file'] = [];
  113. $exif = exif_imagetype($img);
  114. if($exif == false) {
  115. $err = array('code' => errcode::ErrUpfile,"不是图片格式文件");
  116. return false;
  117. }
  118. $mime_type = image_type_to_mime_type($exif);
  119. $_FILES['file']['type'] = $mime_type;
  120. $_FILES['file']['error'] = 0;
  121. $info = new SplFileInfo($img);
  122. $_FILES['file']['size'] = $info->getSize();
  123. $_FILES['file']['name'] = $info->getFilename();
  124. $_FILES['file']['tmp_name'] = $img;
  125. $ext = $info->getExtension();
  126. if(empty($ext)) {
  127. if($name_code=='member'){
  128. $file_name = $member_id.'_'.md5($info->getFilename());
  129. }elseif($name_code=='room'){
  130. $room_id = $member_id;
  131. $file_name = $room_id;
  132. $this->file_name = $file_name;
  133. }
  134. } else {
  135. if($name_code=='member'){
  136. $file_name = $member_id.'_'.md5($info->getFilename()) . '.' . $ext;
  137. }elseif($name_code=='room'){
  138. $room_id = $member_id;
  139. $file_name = $room_id . '.' . $ext;
  140. $this->file_name = $file_name;
  141. }
  142. }
  143. return $file_name;
  144. }
  145. /**
  146. * 设置
  147. *
  148. * @param mixed $key
  149. * @param mixed $value
  150. */
  151. public function set($key,$value){
  152. $this->$key = $value;
  153. }
  154. /**
  155. * 读取
  156. */
  157. public function get($key){
  158. return $this->$key;
  159. }
  160. /**
  161. * 上传操作
  162. *
  163. * @param string $field 上传表单名
  164. * @return bool
  165. */
  166. public function upfile($field){
  167. //上传文件
  168. $this->upload_file = $_FILES[$field];
  169. if ($this->upload_file['tmp_name'] == ""){
  170. $this->setError(Language::get('cant_find_temporary_files'));
  171. return false;
  172. }
  173. //对上传文件错误码进行验证
  174. $error = $this->fileInputError();
  175. if (!$error){
  176. return false;
  177. }
  178. //验证文件大小
  179. if ($this->upload_file['size']==0){
  180. $error = Language::get('upload_file_size_none');
  181. $this->setError($error);
  182. return false;
  183. }
  184. if($this->upload_file['size'] > $this->max_size*1024){
  185. $error = Language::get('upload_file_size_cant_over').$this->max_size.'KB';
  186. $this->setError($error);
  187. return false;
  188. }
  189. //文件后缀名
  190. $tmp_ext = explode(".", $this->upload_file['name']);
  191. $tmp_ext = $tmp_ext[count($tmp_ext) - 1];
  192. $this->ext = strtolower($tmp_ext);
  193. //验证文件格式是否为系统允许
  194. if(!in_array($this->ext,$this->allow_type)){
  195. $error = Language::get('image_allow_ext_is').implode(',',$this->allow_type);
  196. $this->setError($error);
  197. return false;
  198. }
  199. //检查是否为有效图片
  200. if(!$image_info = @getimagesize($this->upload_file['tmp_name'])) {
  201. $error = Language::get('upload_image_is_not_image');
  202. $this->setError($error);
  203. return false;
  204. }
  205. //设置图片路径
  206. $this->save_path = $this->setPath();
  207. //设置文件名称
  208. if(empty($this->file_name)){
  209. $this->setFileName();
  210. }
  211. //是否需要生成缩略图
  212. $ifresize = false;
  213. if ($this->thumb_width && $this->thumb_height && $this->thumb_ext){
  214. $thumb_width = explode(',',$this->thumb_width);
  215. $thumb_height = explode(',',$this->thumb_height);
  216. $thumb_ext = explode(',',$this->thumb_ext);
  217. if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) $ifresize = true;
  218. }
  219. //计算缩略图的尺寸
  220. if ($ifresize)
  221. {
  222. for ($i=0;$i<count($thumb_width);$i++)
  223. {
  224. $imgscaleto = ($thumb_width[$i] == $thumb_height[$i]);
  225. if ($image_info[0] < $thumb_width[$i]) $thumb_width[$i] = $image_info[0];
  226. if ($image_info[1] < $thumb_height[$i]) $thumb_height[$i] = $image_info[1];
  227. $thumb_wh = $thumb_width[$i]/$thumb_height[$i];
  228. $src_wh = $image_info[0]/$image_info[1];
  229. if ($thumb_wh <= $src_wh){
  230. $thumb_height[$i] = $thumb_width[$i]*($image_info[1]/$image_info[0]);
  231. }else{
  232. $thumb_width[$i] = $thumb_height[$i]*($image_info[0]/$image_info[1]);
  233. }
  234. if ($imgscaleto){
  235. $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i];
  236. }else{
  237. $scale[$i] = 0;
  238. }
  239. }
  240. }
  241. if ($this->error != '') return false;
  242. if($this->copy_file($this->upload_file['tmp_name'],BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name))
  243. {
  244. //产生缩略图
  245. if ($ifresize){
  246. $resizeImage = new ResizeImage();
  247. $save_path = rtrim(BASE_UPLOAD_PATH.DS.$this->save_path,'/');
  248. for ($i=0;$i<count($thumb_width);$i++){
  249. $resizeImage->newImg($save_path.DS.$this->file_name,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',$save_path,$this->filling);
  250. if ($i==0) {
  251. $resize_image = explode('/',$resizeImage->relative_dstimg);
  252. $this->thumb_image = $resize_image[count($resize_image)-1];
  253. }
  254. }
  255. }
  256. //删除原图
  257. if ($this->ifremove && is_file(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name)) {
  258. @unlink(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name);
  259. }
  260. return true;
  261. }
  262. else {
  263. $this->setError(Language::get('upload_file_fail'));
  264. return false;
  265. }
  266. return $this->error;
  267. }
  268. private function copy_file($src,$dst)
  269. {
  270. if (!copy($src,$dst)) {
  271. @unlink($src);
  272. return false;
  273. } else {
  274. @unlink($src);
  275. return true;
  276. }
  277. }
  278. /**
  279. * 裁剪指定图片
  280. *
  281. * @param string $field 上传表单名
  282. * @return bool
  283. */
  284. public function create_thumb($pic_path){
  285. if (!file_exists($pic_path)) return ;
  286. //是否需要生成缩略图
  287. $ifresize = false;
  288. if ($this->thumb_width && $this->thumb_height && $this->thumb_ext){
  289. $thumb_width = explode(',',$this->thumb_width);
  290. $thumb_height = explode(',',$this->thumb_height);
  291. $thumb_ext = explode(',',$this->thumb_ext);
  292. if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) $ifresize = true;
  293. }
  294. $image_info = @getimagesize($pic_path);
  295. //计算缩略图的尺寸
  296. if ($ifresize){
  297. for ($i=0;$i<count($thumb_width);$i++){
  298. $imgscaleto = ($thumb_width[$i] == $thumb_height[$i]);
  299. if ($image_info[0] < $thumb_width[$i]) $thumb_width[$i] = $image_info[0];
  300. if ($image_info[1] < $thumb_height[$i]) $thumb_height[$i] = $image_info[1];
  301. $thumb_wh = $thumb_width[$i]/$thumb_height[$i];
  302. $src_wh = $image_info[0]/$image_info[1];
  303. if ($thumb_wh <= $src_wh){
  304. $thumb_height[$i] = $thumb_width[$i]*($image_info[1]/$image_info[0]);
  305. }else{
  306. $thumb_width[$i] = $thumb_height[$i]*($image_info[0]/$image_info[1]);
  307. }
  308. if ($imgscaleto){
  309. $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i];
  310. }else{
  311. $scale[$i] = 0;
  312. }
  313. }
  314. }
  315. //产生缩略图
  316. if ($ifresize)
  317. {
  318. $resizeImage = new ResizeImage();
  319. $save_path = rtrim(BASE_UPLOAD_PATH.DS.$this->save_path,'/');
  320. for ($i=0;$i<count($thumb_width);$i++)
  321. {
  322. $resizeImage->newImg($pic_path,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',dirname($pic_path),$this->filling);
  323. }
  324. }
  325. }
  326. /**
  327. * 获取上传文件的错误信息
  328. *
  329. * @param string $field 上传文件数组键值
  330. * @return string 返回字符串错误信息
  331. */
  332. private function fileInputError(){
  333. switch($this->upload_file['error']) {
  334. case 0:
  335. //文件上传成功
  336. return true;
  337. break;
  338. case 1:
  339. //上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
  340. $this->setError(Language::get('upload_file_size_over'));
  341. return false;
  342. break;
  343. case 2:
  344. //上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
  345. $this->setError(Language::get('upload_file_size_over'));
  346. return false;
  347. break;
  348. case 3:
  349. //文件只有部分被上传
  350. $this->setError(Language::get('upload_file_is_not_complete'));
  351. return false;
  352. break;
  353. case 4:
  354. //没有文件被上传
  355. $this->setError(Language::get('upload_file_is_not_uploaded'));
  356. return false;
  357. break;
  358. case 6:
  359. //找不到临时文件夹
  360. $this->setError(Language::get('upload_dir_chmod'));
  361. return false;
  362. break;
  363. case 7:
  364. //文件写入失败
  365. $this->setError(Language::get('upload_file_write_fail'));
  366. return false;
  367. break;
  368. default:
  369. return true;
  370. }
  371. }
  372. /**
  373. * 设置存储路径
  374. *
  375. * @return string 字符串形式的返回结果
  376. */
  377. public function setPath(){
  378. /**
  379. * 判断目录是否存在,如果不存在 则生成
  380. */
  381. if (!is_dir(BASE_UPLOAD_PATH.DS.$this->default_dir)){
  382. $dir = $this->default_dir;
  383. $dir_array = explode(DS,$dir);
  384. $tmp_base_path = BASE_UPLOAD_PATH;
  385. foreach ($dir_array as $k => $v){
  386. $tmp_base_path = $tmp_base_path.DS.$v;
  387. if(!is_dir($tmp_base_path)){
  388. if (!@mkdir($tmp_base_path,0755,true)){
  389. $this->setError('创建目录失败,请检查是否有写入权限');
  390. return false;
  391. }
  392. }
  393. }
  394. unset($dir,$dir_array,$tmp_base_path);
  395. }
  396. //设置权限
  397. //@chmod(BASE_UPLOAD_PATH.DS.$this->default_dir,0755);
  398. @chmod(BASE_UPLOAD_PATH.DS.$this->default_dir,0777);
  399. //判断文件夹是否可写
  400. if(!is_writable(BASE_UPLOAD_PATH.DS.$this->default_dir)) {
  401. $this->setError(Language::get('upload_file_dir').$this->default_dir.Language::get('upload_file_dir_cant_touch_file'));
  402. return false;
  403. }
  404. return $this->default_dir;
  405. }
  406. /**
  407. * 设置文件名称 不包括 文件路径
  408. *
  409. * 生成(从2000-01-01 00:00:00 到现在的秒数+微秒+四位随机)
  410. */
  411. private function setFileName(){
  412. $tmp_name = sprintf('%010d',time() - 946656000)
  413. . sprintf('%03d', microtime() * 1000)
  414. . sprintf('%04d', mt_rand(0,9999));
  415. $this->file_name = (empty ( $this->fprefix ) ? '' : $this->fprefix . '_')
  416. . $tmp_name . '.' . ($this->new_ext == '' ? $this->ext : $this->new_ext);
  417. }
  418. /**
  419. * 设置错误信息
  420. *
  421. * @param string $error 错误信息
  422. * @return bool 布尔类型的返回结果
  423. */
  424. private function setError($error){
  425. $this->error = $error;
  426. }
  427. /**
  428. * 根据系统设置返回商品图片保存路径
  429. */
  430. public function getSysSetPath(){
  431. switch(C('image_dir_type')){
  432. case "1":
  433. //按文件类型存放,例如/a.jpg
  434. $subpath = "";
  435. break;
  436. case "2":
  437. //按上传年份存放,例如2011/a.jpg
  438. $subpath = date("Y",time()) . "/";
  439. break;
  440. case "3":
  441. //按上传年月存放,例如2011/04/a.jpg
  442. $subpath = date("Y",time()) . "/" . date("m",time()) . "/";
  443. break;
  444. case "4":
  445. //按上传年月日存放,例如2011/04/19/a.jpg
  446. $subpath = date("Y",time()) . "/" . date("m",time()) . "/" . date("d",time()) . "/";
  447. }
  448. return $subpath;
  449. }
  450. }