upload_file.php 15 KB

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