uploadfile.php 14 KB

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