Exemplo n.º 1
0
 /**
  * 函数说明: 截取文件Mime类型
  * 
  * @param string field 文件域名称
  * @return string /bool(false)
  */
 public static function get_file_type($field)
 {
     if (isset($_FILES[$field]) && !empty($_FILES[$field]['type'])) {
         if (!is_array($_FILES[$field]['type'])) {
             if (!isset(kc_page::$mimemap) || empty(kc_page::$mimemap)) {
                 kc_page::$mimemap = Kohana::config('mimemap.type2postfix');
             }
             if (array_key_exists($_FILES[$field]['type'], kc_page::$mimemap)) {
                 return kc_page::$mimemap[$_FILES[$field]['type']];
             } else {
                 return false;
             }
         } else {
             if (!isset(kc_page::$mimemap) || empty(kc_page::$mimemap)) {
                 kc_page::$mimemap = Kohana::config('mimemap.type2postfix');
             }
             if (array_key_exists($_FILES[$field]['type'], kc_page::$mimemap)) {
                 return kc_page::$mimemap[$_FILES[$field]['type']];
             } else {
                 return false;
             }
         }
     } else {
         return false;
     }
 }
Exemplo n.º 2
0
 /**
  * 文件上传处理
  * @param int $folder_id
  * @return array attachment_ids
  * @author weizhifeng
  **/
 protected function do_upload($folder_id)
 {
     // 上传的表单域名字
     $attach_field = 'upload';
     // 附件应用类型
     $attach_app_type = 'productPicAttach';
     $file_count_total = 0;
     $file_size_total = 0;
     // 上传文件meta信息
     $file_meta_data = array();
     // 如果有上传请求
     if (!isset($_FILES[$attach_field]) || empty($_FILES[$attach_field])) {
         exit(Kohana::lang('o_global.bad_request'));
     }
     $title = array();
     //读取当前应用配置
     $attach_setup = Kohana::config('attach.' . $attach_app_type);
     $mime_type2postfix = Kohana::config('mimemap.type2postfix');
     $mime_postfix2type = Kohana::config('mimemap.postfix2type');
     $file_type_current = FALSE;
     $file_size_current = '';
     $file_mime_current = '';
     // 如果上传标志成功
     if ((int) $_FILES[$attach_field]['error'] === UPLOAD_ERR_OK) {
         if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'])) {
             $this->error_msg(Kohana::lang('o_promotion.file_not_uploaded'));
         }
         $file_size_current = filesize($_FILES[$attach_field]['tmp_name']);
         if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) {
             $this->error_msg(Kohana::lang('o_kc.file_big_than_max', $attach_setup['fileSizePreLimit'] / 1024 / 1024));
         }
         // 尝试通过图片类型判断
         $file_type_current = $file_type_current ? $file_type_current : kc_page::get_image_type($_FILES[$attach_field]['tmp_name']);
         // 尝试通过Mime类型判断
         $file_type_current = $file_type_current ? $file_type_current : kc_page::get_file_type($attach_field);
         // 尝试通过后缀截取
         $file_type_current = $file_type_current ? $file_type_current : kc_page::get_postfix($attach_field);
         if (!empty($attach_setup['allowTypes']) && !in_array($file_type_current, $attach_setup['allowTypes'])) {
             $this->error_msg(Kohana::lang('o_promotion.file_type_invalid'));
         }
         // 当前文件mime类型
         $file_mime_current = isset($_FILES[$attach_field]['type']) ? $_FILES[$attach_field]['type'] : '';
         // 检测规整mime类型
         if (!array_key_exists($file_mime_current, $mime_type2postfix)) {
             if (array_key_exists($file_type_current, $mime_postfix2type)) {
                 $file_mime_current = $mime_postfix2type[$file_type_current];
             } else {
                 $file_mime_current = 'application/octet-stream';
             }
         }
         //存储文件meta信息
         $file_meta_data = array('site_id' => $this->site_id, 'name' => strip_tags(trim($_FILES[$attach_field]['name'])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmpfile' => $_FILES[$attach_field]['tmp_name']);
         // 设置上传总数量
         $file_count_total += 1;
         // 设置上传总大小
         $file_size_total += $file_size_current;
     } else {
         $this->error_msg(Kohana::lang('o_product.pic_upload_failed'));
     }
     if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) {
         $this->error_msg(Kohana::lang('o_promotion.file_count_limit') . $attach_setup['fileCountLimit']);
     }
     if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) {
         $this->error_msg(Kohana::lang('o_promotion.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_promotion.size') . $file_size_total);
     }
     $att = AttService::get_instance($this->img_dir_name);
     $attachment_id = $att->save_default_img($file_meta_data['tmpfile']);
     if (!$attachment_id) {
         throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 500);
     }
     /*$attachment_id = Mykc_image::instance()->add_attach($file_meta_data);
       if (!is_numeric($attachment_id))
       {
           $this->error_msg(Kohana::lang('o_product.phprpc_pic_save_failed'));
       }*/
     $image_name = strip_tags(trim($_FILES[$attach_field]['name']));
     while (Mykc_image::instance()->count(array('where' => array('site_id' => $this->site_id, 'kc_folder_id' => $folder_id, 'image_name' => $image_name))) >= 1) {
         $point_pos = strrpos($image_name, '.');
         if ($point_pos !== FALSE) {
             $postfix = substr($image_name, $point_pos + 1);
             $pre_name = substr($image_name, 0, $point_pos);
             $image_name = $pre_name . '_' . $attachment_id . '.' . $postfix;
         } else {
             $image_name = $image_name . '_' . $attachment_id;
         }
     }
     $image_data = array('site_id' => $this->site_id, 'kc_folder_id' => $folder_id, 'attach_id' => $attachment_id, 'image_type' => $file_type_current, 'image_size' => $file_size_current, 'image_name' => $image_name, 'image_mime' => $file_mime_current, 'date_add' => date('Y-m-d H:i:s'), 'date_upd' => date('Y-m-d H:i:s'));
     $image_id = Mykc_image::instance()->create($image_data);
     if (!$image_id) {
         exit(Kohana::lang('o_kc.can_not_write_folder'));
     }
     return $attachment_id;
 }