upload_control.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. class upload_control
  3. {
  4. private $store;
  5. private $store_grade;
  6. public function __construct()
  7. {
  8. }
  9. public function set($key,$value) {
  10. $this->$key = $value;
  11. }
  12. public function set_store($storeid)
  13. {
  14. $this->store = $storeid;
  15. }
  16. /**
  17. * 读取
  18. */
  19. public function get($key){
  20. return $this->$key;
  21. }
  22. const POST_NAME = 'goods_image';
  23. private function fill_files($filename)
  24. {
  25. if(file_exists(fetch_config::tmp_file)) {
  26. unlink(fetch_config::tmp_file);
  27. }
  28. copy($filename,fetch_config::tmp_file);
  29. if(empty($_FILES['goods_image'])) {
  30. $_FILES['goods_image'] = array();
  31. }
  32. $post_info = &$_FILES['goods_image'];
  33. $path_info = pathinfo($filename);
  34. $post_info['path'] = $filename;
  35. $post_info['name'] = $path_info['basename'];
  36. $post_info['type'] = 'image/'.$path_info['extension'];
  37. $post_info['error'] = 0;
  38. $post_info['size'] = filesize($filename);
  39. $post_info['tmp_name'] = fetch_config::tmp_file;
  40. }
  41. public function upload_img($file)
  42. {
  43. // 判断图片数量是否超限
  44. $model_album = Model('album');
  45. // 上传图片
  46. $upload = new upload_file();
  47. $upload->set('default_dir', ATTACH_GOODS . DS . $this->store . DS . $upload->getSysSetPath());
  48. $upload->set('max_size', C('image_max_filesize'));
  49. $upload->set('thumb_width', GOODS_IMAGES_WIDTH);
  50. $upload->set('thumb_height', GOODS_IMAGES_HEIGHT);
  51. $upload->set('thumb_ext', GOODS_IMAGES_EXT);
  52. $upload->set('fprefix', $this->store);
  53. $upload->set('allow_type', array('gif', 'jpg', 'jpeg', 'png'));
  54. $this->fill_files($file);
  55. $result = $upload->upfile(upload_control::POST_NAME);
  56. if (!$result) {
  57. if (strtoupper(CHARSET) == 'GBK') {
  58. $upload->error = Language::getUTF8($upload->error);
  59. }
  60. $output = array();
  61. $output['error'] = $upload->error;
  62. $output = json_encode($output);
  63. if(is_mobile()) {
  64. return false;
  65. }
  66. else {
  67. exit($output);
  68. }
  69. }
  70. $img_path = $upload->getSysSetPath() . $upload->file_name;
  71. // 取得图像大小
  72. list($width, $height, $type, $attr) = getimagesize(BASE_UPLOAD_PATH . '/' . ATTACH_GOODS . '/' . $this->store . DS . $img_path);
  73. // 存入相册
  74. $image = explode('.', $_FILES[upload_control::POST_NAME]["name"]);
  75. $insert_array = array();
  76. $insert_array['apic_name'] = $image['0'];
  77. $insert_array['apic_tag'] = '';
  78. $insert_array['aclass_id'] = $this->store;//$class_info['aclass_id'];
  79. $insert_array['apic_cover'] = $img_path;
  80. $insert_array['apic_size'] = intval($_FILES[upload_control::POST_NAME]['size']);
  81. $insert_array['apic_spec'] = $width . 'x' . $height;
  82. $insert_array['upload_time'] = time();
  83. $insert_array['store_id'] = $this->store;
  84. $model_album->addPic($insert_array);
  85. return $upload->file_name;
  86. }
  87. }