uploadfile.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. Log::record("UploadFile upfile {$xxx_name}, {$xxx_path}",Log::DEBUG);
  247. if(move_uploaded_file($this->upload_file['tmp_name'],BASE_UPLOAD_PATH.DS.$this->save_path.$this->file_name))
  248. {
  249. Log::record("UploadFile upfile 12 1",Log::DEBUG);
  250. //产生缩略图
  251. if ($ifresize){
  252. $resizeImage = new ResizeImage();
  253. $save_path = rtrim(BASE_UPLOAD_PATH.DS.$this->save_path,'/');
  254. for ($i=0;$i<count($thumb_width);$i++){
  255. $resizeImage->newImg($save_path.DS.$this->file_name,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',$save_path,$this->filling);
  256. if ($i==0) {
  257. $resize_image = explode('/',$resizeImage->relative_dstimg);
  258. $this->thumb_image = $resize_image[count($resize_image)-1];
  259. }
  260. }
  261. }
  262. Log::record("UploadFile upfile 13",Log::DEBUG);
  263. //删除原图
  264. if ($this->ifremove && is_file(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name)) {
  265. unlink(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name);
  266. }
  267. return true;
  268. } else {
  269. $this->setError(Language::get('upload_file_fail'));
  270. return false;
  271. }
  272. // $this->setErrorFileName($this->upload_file['tmp_name']);
  273. return $this->error;
  274. }
  275. /**
  276. * 裁剪指定图片
  277. *
  278. * @param string $field 上传表单名
  279. * @return bool
  280. */
  281. public function create_thumb($pic_path){
  282. if (!file_exists($pic_path)) return ;
  283. //是否需要生成缩略图
  284. $ifresize = false;
  285. if ($this->thumb_width && $this->thumb_height && $this->thumb_ext){
  286. $thumb_width = explode(',',$this->thumb_width);
  287. $thumb_height = explode(',',$this->thumb_height);
  288. $thumb_ext = explode(',',$this->thumb_ext);
  289. if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) $ifresize = true;
  290. }
  291. $image_info = getimagesize($pic_path);
  292. //计算缩略图的尺寸
  293. if ($ifresize){
  294. for ($i=0;$i<count($thumb_width);$i++){
  295. $imgscaleto = ($thumb_width[$i] == $thumb_height[$i]);
  296. if ($image_info[0] < $thumb_width[$i]) $thumb_width[$i] = $image_info[0];
  297. if ($image_info[1] < $thumb_height[$i]) $thumb_height[$i] = $image_info[1];
  298. $thumb_wh = $thumb_width[$i]/$thumb_height[$i];
  299. $src_wh = $image_info[0]/$image_info[1];
  300. if ($thumb_wh <= $src_wh){
  301. $thumb_height[$i] = $thumb_width[$i]*($image_info[1]/$image_info[0]);
  302. }else{
  303. $thumb_width[$i] = $thumb_height[$i]*($image_info[0]/$image_info[1]);
  304. }
  305. if ($imgscaleto){
  306. $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i];
  307. }else{
  308. $scale[$i] = 0;
  309. }
  310. }
  311. }
  312. //产生缩略图
  313. if ($ifresize){
  314. $resizeImage = new ResizeImage();
  315. $save_path = rtrim(BASE_UPLOAD_PATH.DS.$this->save_path,'/');
  316. for ($i=0;$i<count($thumb_width);$i++){
  317. // $resizeImage->newImg($save_path.DS.$this->file_name,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',$save_path,$this->filling);
  318. $resizeImage->newImg($pic_path,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',dirname($pic_path),$this->filling);
  319. }
  320. }
  321. }
  322. /**
  323. * 获取上传文件的错误信息
  324. *
  325. * @param string $field 上传文件数组键值
  326. * @return string 返回字符串错误信息
  327. */
  328. private function fileInputError(){
  329. switch($this->upload_file['error']) {
  330. case 0:
  331. //文件上传成功
  332. return true;
  333. break;
  334. case 1:
  335. //上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
  336. $this->setError(Language::get('upload_file_size_over'));
  337. return false;
  338. break;
  339. case 2:
  340. //上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
  341. $this->setError(Language::get('upload_file_size_over'));
  342. return false;
  343. break;
  344. case 3:
  345. //文件只有部分被上传
  346. $this->setError(Language::get('upload_file_is_not_complete'));
  347. return false;
  348. break;
  349. case 4:
  350. //没有文件被上传
  351. $this->setError(Language::get('upload_file_is_not_uploaded'));
  352. return false;
  353. break;
  354. case 6:
  355. //找不到临时文件夹
  356. $this->setError(Language::get('upload_dir_chmod'));
  357. return false;
  358. break;
  359. case 7:
  360. //文件写入失败
  361. $this->setError(Language::get('upload_file_write_fail'));
  362. return false;
  363. break;
  364. default:
  365. return true;
  366. }
  367. }
  368. /**
  369. * 设置存储路径
  370. *
  371. * @return string 字符串形式的返回结果
  372. */
  373. public function setPath(){
  374. /**
  375. * 判断目录是否存在,如果不存在 则生成
  376. */
  377. if (!is_dir(BASE_UPLOAD_PATH.DS.$this->default_dir)){
  378. $dir = $this->default_dir;
  379. $dir_array = explode(DS,$dir);
  380. $tmp_base_path = BASE_UPLOAD_PATH;
  381. foreach ($dir_array as $k => $v){
  382. $tmp_base_path = $tmp_base_path.DS.$v;
  383. if(!is_dir($tmp_base_path)){
  384. if (!mkdir($tmp_base_path,0755,true)){
  385. $this->setError('创建目录失败,请检查是否有写入权限');
  386. return false;
  387. }
  388. }
  389. }
  390. unset($dir,$dir_array,$tmp_base_path);
  391. }
  392. //设置权限
  393. @chmod(BASE_UPLOAD_PATH.DS.$this->default_dir,0755);
  394. //判断文件夹是否可写
  395. if(!is_writable(BASE_UPLOAD_PATH.DS.$this->default_dir)) {
  396. $this->setError(Language::get('upload_file_dir').$this->default_dir.Language::get('upload_file_dir_cant_touch_file'));
  397. return false;
  398. }
  399. return $this->default_dir;
  400. }
  401. /**
  402. * 设置文件名称 不包括 文件路径
  403. *
  404. * 生成(从2000-01-01 00:00:00 到现在的秒数+微秒+四位随机)
  405. */
  406. private function setFileName(){
  407. $tmp_name = sprintf('%010d',time() - 946656000)
  408. . sprintf('%03d', microtime() * 1000)
  409. . sprintf('%04d', mt_rand(0,9999));
  410. $this->file_name = (empty ( $this->fprefix ) ? '' : $this->fprefix . '_')
  411. . $tmp_name . '.' . ($this->new_ext == '' ? $this->ext : $this->new_ext);
  412. }
  413. /**
  414. * 设置错误信息
  415. *
  416. * @param string $error 错误信息
  417. * @return bool 布尔类型的返回结果
  418. */
  419. private function setError($error){
  420. $this->error = $error;
  421. }
  422. /**
  423. * 根据系统设置返回商品图片保存路径
  424. */
  425. public function getSysSetPath(){
  426. switch(C('image_dir_type')){
  427. case "1":
  428. //按文件类型存放,例如/a.jpg
  429. $subpath = "";
  430. break;
  431. case "2":
  432. //按上传年份存放,例如2011/a.jpg
  433. $subpath = date("Y",time()) . "/";
  434. break;
  435. case "3":
  436. //按上传年月存放,例如2011/04/a.jpg
  437. $subpath = date("Y",time()) . "/" . date("m",time()) . "/";
  438. break;
  439. case "4":
  440. //按上传年月日存放,例如2011/04/19/a.jpg
  441. $subpath = date("Y",time()) . "/" . date("m",time()) . "/" . date("d",time()) . "/";
  442. }
  443. return $subpath;
  444. }
  445. }