upload_file.php 15 KB

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