FileUploader.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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)
  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. $file_name = $member_id.'_'.md5($info->getFilename());
  128. } else {
  129. $file_name = $member_id.'_'.md5($info->getFilename()) . '.' . $ext;
  130. }
  131. return $file_name;
  132. }
  133. /**
  134. * 设置
  135. *
  136. * @param mixed $key
  137. * @param mixed $value
  138. */
  139. public function set($key,$value){
  140. $this->$key = $value;
  141. }
  142. /**
  143. * 读取
  144. */
  145. public function get($key){
  146. return $this->$key;
  147. }
  148. /**
  149. * 上传操作
  150. *
  151. * @param string $field 上传表单名
  152. * @return bool
  153. */
  154. public function upfile($field){
  155. //上传文件
  156. $this->upload_file = $_FILES[$field];
  157. if ($this->upload_file['tmp_name'] == ""){
  158. $this->setError(Language::get('cant_find_temporary_files'));
  159. return false;
  160. }
  161. //对上传文件错误码进行验证
  162. $error = $this->fileInputError();
  163. if (!$error){
  164. return false;
  165. }
  166. //验证文件大小
  167. if ($this->upload_file['size']==0){
  168. $error = Language::get('upload_file_size_none');
  169. $this->setError($error);
  170. return false;
  171. }
  172. if($this->upload_file['size'] > $this->max_size*1024){
  173. $error = Language::get('upload_file_size_cant_over').$this->max_size.'KB';
  174. $this->setError($error);
  175. return false;
  176. }
  177. //文件后缀名
  178. $tmp_ext = explode(".", $this->upload_file['name']);
  179. $tmp_ext = $tmp_ext[count($tmp_ext) - 1];
  180. $this->ext = strtolower($tmp_ext);
  181. //验证文件格式是否为系统允许
  182. if(!in_array($this->ext,$this->allow_type)){
  183. $error = Language::get('image_allow_ext_is').implode(',',$this->allow_type);
  184. $this->setError($error);
  185. return false;
  186. }
  187. //检查是否为有效图片
  188. if(!$image_info = @getimagesize($this->upload_file['tmp_name'])) {
  189. $error = Language::get('upload_image_is_not_image');
  190. $this->setError($error);
  191. return false;
  192. }
  193. //设置图片路径
  194. $this->save_path = $this->setPath();
  195. //设置文件名称
  196. if(empty($this->file_name)){
  197. $this->setFileName();
  198. }
  199. //是否需要生成缩略图
  200. $ifresize = false;
  201. if ($this->thumb_width && $this->thumb_height && $this->thumb_ext){
  202. $thumb_width = explode(',',$this->thumb_width);
  203. $thumb_height = explode(',',$this->thumb_height);
  204. $thumb_ext = explode(',',$this->thumb_ext);
  205. if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) $ifresize = true;
  206. }
  207. //计算缩略图的尺寸
  208. if ($ifresize)
  209. {
  210. for ($i=0;$i<count($thumb_width);$i++)
  211. {
  212. $imgscaleto = ($thumb_width[$i] == $thumb_height[$i]);
  213. if ($image_info[0] < $thumb_width[$i]) $thumb_width[$i] = $image_info[0];
  214. if ($image_info[1] < $thumb_height[$i]) $thumb_height[$i] = $image_info[1];
  215. $thumb_wh = $thumb_width[$i]/$thumb_height[$i];
  216. $src_wh = $image_info[0]/$image_info[1];
  217. if ($thumb_wh <= $src_wh){
  218. $thumb_height[$i] = $thumb_width[$i]*($image_info[1]/$image_info[0]);
  219. }else{
  220. $thumb_width[$i] = $thumb_height[$i]*($image_info[0]/$image_info[1]);
  221. }
  222. if ($imgscaleto){
  223. $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i];
  224. }else{
  225. $scale[$i] = 0;
  226. }
  227. }
  228. }
  229. if ($this->error != '') return false;
  230. if($this->copy_file($this->upload_file['tmp_name'],BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name))
  231. {
  232. //产生缩略图
  233. if ($ifresize){
  234. $resizeImage = new ResizeImage();
  235. $save_path = rtrim(BASE_UPLOAD_PATH.DS.$this->save_path,'/');
  236. for ($i=0;$i<count($thumb_width);$i++){
  237. $resizeImage->newImg($save_path.DS.$this->file_name,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',$save_path,$this->filling);
  238. if ($i==0) {
  239. $resize_image = explode('/',$resizeImage->relative_dstimg);
  240. $this->thumb_image = $resize_image[count($resize_image)-1];
  241. }
  242. }
  243. }
  244. //删除原图
  245. if ($this->ifremove && is_file(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name)) {
  246. @unlink(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name);
  247. }
  248. return true;
  249. }
  250. else {
  251. $this->setError(Language::get('upload_file_fail'));
  252. return false;
  253. }
  254. return $this->error;
  255. }
  256. private function copy_file($src,$dst)
  257. {
  258. if (!copy($src,$dst)) {
  259. @unlink($src);
  260. return false;
  261. } else {
  262. @unlink($src);
  263. return true;
  264. }
  265. }
  266. /**
  267. * 裁剪指定图片
  268. *
  269. * @param string $field 上传表单名
  270. * @return bool
  271. */
  272. public function create_thumb($pic_path){
  273. if (!file_exists($pic_path)) return ;
  274. //是否需要生成缩略图
  275. $ifresize = false;
  276. if ($this->thumb_width && $this->thumb_height && $this->thumb_ext){
  277. $thumb_width = explode(',',$this->thumb_width);
  278. $thumb_height = explode(',',$this->thumb_height);
  279. $thumb_ext = explode(',',$this->thumb_ext);
  280. if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) $ifresize = true;
  281. }
  282. $image_info = @getimagesize($pic_path);
  283. //计算缩略图的尺寸
  284. if ($ifresize){
  285. for ($i=0;$i<count($thumb_width);$i++){
  286. $imgscaleto = ($thumb_width[$i] == $thumb_height[$i]);
  287. if ($image_info[0] < $thumb_width[$i]) $thumb_width[$i] = $image_info[0];
  288. if ($image_info[1] < $thumb_height[$i]) $thumb_height[$i] = $image_info[1];
  289. $thumb_wh = $thumb_width[$i]/$thumb_height[$i];
  290. $src_wh = $image_info[0]/$image_info[1];
  291. if ($thumb_wh <= $src_wh){
  292. $thumb_height[$i] = $thumb_width[$i]*($image_info[1]/$image_info[0]);
  293. }else{
  294. $thumb_width[$i] = $thumb_height[$i]*($image_info[0]/$image_info[1]);
  295. }
  296. if ($imgscaleto){
  297. $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i];
  298. }else{
  299. $scale[$i] = 0;
  300. }
  301. }
  302. }
  303. //产生缩略图
  304. if ($ifresize)
  305. {
  306. $resizeImage = new ResizeImage();
  307. $save_path = rtrim(BASE_UPLOAD_PATH.DS.$this->save_path,'/');
  308. for ($i=0;$i<count($thumb_width);$i++)
  309. {
  310. $resizeImage->newImg($pic_path,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',dirname($pic_path),$this->filling);
  311. }
  312. }
  313. }
  314. /**
  315. * 获取上传文件的错误信息
  316. *
  317. * @param string $field 上传文件数组键值
  318. * @return string 返回字符串错误信息
  319. */
  320. private function fileInputError(){
  321. switch($this->upload_file['error']) {
  322. case 0:
  323. //文件上传成功
  324. return true;
  325. break;
  326. case 1:
  327. //上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
  328. $this->setError(Language::get('upload_file_size_over'));
  329. return false;
  330. break;
  331. case 2:
  332. //上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
  333. $this->setError(Language::get('upload_file_size_over'));
  334. return false;
  335. break;
  336. case 3:
  337. //文件只有部分被上传
  338. $this->setError(Language::get('upload_file_is_not_complete'));
  339. return false;
  340. break;
  341. case 4:
  342. //没有文件被上传
  343. $this->setError(Language::get('upload_file_is_not_uploaded'));
  344. return false;
  345. break;
  346. case 6:
  347. //找不到临时文件夹
  348. $this->setError(Language::get('upload_dir_chmod'));
  349. return false;
  350. break;
  351. case 7:
  352. //文件写入失败
  353. $this->setError(Language::get('upload_file_write_fail'));
  354. return false;
  355. break;
  356. default:
  357. return true;
  358. }
  359. }
  360. /**
  361. * 设置存储路径
  362. *
  363. * @return string 字符串形式的返回结果
  364. */
  365. public function setPath(){
  366. /**
  367. * 判断目录是否存在,如果不存在 则生成
  368. */
  369. if (!is_dir(BASE_UPLOAD_PATH.DS.$this->default_dir)){
  370. $dir = $this->default_dir;
  371. $dir_array = explode(DS,$dir);
  372. $tmp_base_path = BASE_UPLOAD_PATH;
  373. foreach ($dir_array as $k => $v){
  374. $tmp_base_path = $tmp_base_path.DS.$v;
  375. if(!is_dir($tmp_base_path)){
  376. if (!@mkdir($tmp_base_path,0755,true)){
  377. $this->setError('创建目录失败,请检查是否有写入权限');
  378. return false;
  379. }
  380. }
  381. }
  382. unset($dir,$dir_array,$tmp_base_path);
  383. }
  384. //设置权限
  385. //@chmod(BASE_UPLOAD_PATH.DS.$this->default_dir,0755);
  386. @chmod(BASE_UPLOAD_PATH.DS.$this->default_dir,0777);
  387. //判断文件夹是否可写
  388. if(!is_writable(BASE_UPLOAD_PATH.DS.$this->default_dir)) {
  389. $this->setError(Language::get('upload_file_dir').$this->default_dir.Language::get('upload_file_dir_cant_touch_file'));
  390. return false;
  391. }
  392. return $this->default_dir;
  393. }
  394. /**
  395. * 设置文件名称 不包括 文件路径
  396. *
  397. * 生成(从2000-01-01 00:00:00 到现在的秒数+微秒+四位随机)
  398. */
  399. private function setFileName(){
  400. $tmp_name = sprintf('%010d',time() - 946656000)
  401. . sprintf('%03d', microtime() * 1000)
  402. . sprintf('%04d', mt_rand(0,9999));
  403. $this->file_name = (empty ( $this->fprefix ) ? '' : $this->fprefix . '_')
  404. . $tmp_name . '.' . ($this->new_ext == '' ? $this->ext : $this->new_ext);
  405. }
  406. /**
  407. * 设置错误信息
  408. *
  409. * @param string $error 错误信息
  410. * @return bool 布尔类型的返回结果
  411. */
  412. private function setError($error){
  413. $this->error = $error;
  414. }
  415. /**
  416. * 根据系统设置返回商品图片保存路径
  417. */
  418. public function getSysSetPath(){
  419. switch(C('image_dir_type')){
  420. case "1":
  421. //按文件类型存放,例如/a.jpg
  422. $subpath = "";
  423. break;
  424. case "2":
  425. //按上传年份存放,例如2011/a.jpg
  426. $subpath = date("Y",time()) . "/";
  427. break;
  428. case "3":
  429. //按上传年月存放,例如2011/04/a.jpg
  430. $subpath = date("Y",time()) . "/" . date("m",time()) . "/";
  431. break;
  432. case "4":
  433. //按上传年月日存放,例如2011/04/19/a.jpg
  434. $subpath = date("Y",time()) . "/" . date("m",time()) . "/" . date("d",time()) . "/";
  435. }
  436. return $subpath;
  437. }
  438. }