uploadfile.php 12 KB

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