uploadfile.php 12 KB

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