uploadfile.php 13 KB

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