upload_control.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const tmp_file = '/private/var/tmp/phpxxkkdl';
  24. private function fill_files($filename)
  25. {
  26. if(file_exists(self::tmp_file)) {
  27. unlink(self::tmp_file);
  28. }
  29. copy($filename,self::tmp_file);
  30. if(empty($_FILES['goods_image'])) {
  31. $_FILES['goods_image'] = array();
  32. $post_info = &$_FILES['goods_image'];
  33. }
  34. $path_info = pathinfo($filename);
  35. $post_info['path'] = $filename;
  36. $post_info['name'] = $path_info['basename'];
  37. $post_info['type'] = 'image/'.$path_info['extension'];
  38. $post_info['error'] = 0;
  39. $post_info['size'] = filesize($filename);
  40. $post_info['tmp_name'] = self::tmp_file;
  41. }
  42. public function upload_img($file)
  43. {
  44. // 判断图片数量是否超限
  45. $model_album = Model('album');
  46. // 上传图片
  47. $upload = new upload_file();
  48. $upload->set('default_dir', ATTACH_GOODS . DS . $this->store . DS . $upload->getSysSetPath());
  49. $upload->set('max_size', C('image_max_filesize'));
  50. $upload->set('thumb_width', GOODS_IMAGES_WIDTH);
  51. $upload->set('thumb_height', GOODS_IMAGES_HEIGHT);
  52. $upload->set('thumb_ext', GOODS_IMAGES_EXT);
  53. $upload->set('fprefix', $this->store);
  54. $upload->set('allow_type', array('gif', 'jpg', 'jpeg', 'png'));
  55. $this->fill_files($file);
  56. $result = $upload->upfile(upload_control::POST_NAME);
  57. if (!$result) {
  58. if (strtoupper(CHARSET) == 'GBK') {
  59. $upload->error = Language::getUTF8($upload->error);
  60. }
  61. $output = array();
  62. $output['error'] = $upload->error;
  63. $output = json_encode($output);
  64. exit($output);
  65. }
  66. $img_path = $upload->getSysSetPath() . $upload->file_name;
  67. // 取得图像大小
  68. list($width, $height, $type, $attr) = getimagesize(BASE_UPLOAD_PATH . '/' . ATTACH_GOODS . '/' . $this->store . DS . $img_path);
  69. // 存入相册
  70. $image = explode('.', $_FILES[upload_control::POST_NAME]["name"]);
  71. $insert_array = array();
  72. $insert_array['apic_name'] = $image['0'];
  73. $insert_array['apic_tag'] = '';
  74. $insert_array['aclass_id'] = $this->store;//$class_info['aclass_id'];
  75. $insert_array['apic_cover'] = $img_path;
  76. $insert_array['apic_size'] = intval($_FILES[upload_control::POST_NAME]['size']);
  77. $insert_array['apic_spec'] = $width . 'x' . $height;
  78. $insert_array['upload_time'] = TIMESTAMP;
  79. $insert_array['store_id'] = $this->store;
  80. $model_album->addPic($insert_array);
  81. return $upload->file_name;
  82. }
  83. }