/** * 图片附件显示 */ public function index($img_dir, $img = NULL) { if ($img_dir && !$img) { $img = $img_dir; $img_dir = "att"; } //$img_type='jpg' $imgArr = explode('.', $img); $img = $imgArr[0]; $img_type = isset($imgArr[1]) ? $imgArr[1] : 'jpg'; //$img_id=NULL, $w=0, $h=0 $imgArr = explode('_', $img); $img_id = isset($imgArr[0]) ? $imgArr[0] : NULL; $w = isset($imgArr[1]) ? $imgArr[1] : 0; $h = isset($imgArr[2]) ? $imgArr[2] : 0; //兼容格式:40x40 if (strpos(strtolower($w), 'x')) { $x = explode("x", $w); $w = isset($x[0]) ? $x[0] : 0; $h = isset($x[1]) ? $x[1] : 0; } $filename = AttService::get_instance($img_dir)->get_img_dir($img_id, $w, $h, $img_type); // 尝试通过图片类型判断 $file_type_current = page::getImageType($filename); $file_type_current = $file_type_current == 'jpg' ? 'jpeg' : 'gif'; ob_end_clean(); header("Content-type: image/{$file_type_current}"); @readfile($filename); ob_end_flush(); die; }
/** * 图片显示 */ public function pic($img_name, $img_dir1, $img_dir2, $img_dir3, $img_dir4, $img_filename) { $imgArr = explode('.', $img_filename); $img = $imgArr[0]; $img_type = isset($imgArr[1]) ? $imgArr[1] : 'jpg'; $img_id = $img_dir1 . '/' . $img_dir2 . '/' . $img_dir3 . '/' . $img_dir4 . '/' . $imgArr[0]; $filename = AttService::get_instance($img_name)->get_img_dir($img_id, 0, 0, $img_type); $file_type_current = page::getImageType($filename); $file_type_current = $file_type_current == 'jpg' ? 'jpeg' : 'gif'; ob_end_clean(); header("Content-type: image/{$file_type_current}"); @readfile($filename); ob_end_flush(); exit; }
public function pic_upload() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); $request_data = $this->input->post(); $request_product_id = $request_data['product_id']; // 请求的对应的图片说明 $request_pic_title_assoc = !empty($request_data) && isset($request_data['myPorductpicTitle']) && is_array($request_data['myPorductpicTitle']) && !empty($request_data['myPorductpicTitle']) ? $request_data['myPorductpicTitle'] : array(); //多附件上传 上传的表单域名字 $attach_field = 'myPorductpic'; // 附件应用类型 $attach_app_type = 'productPicAttach'; // 如果有上传请求 if (!page::issetFile($attach_field)) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $file_upload_count = page::getFileCount($attach_field); // 初始化一些数据 // 本次文件上传总数量 $file_count_total = 0; // 本次文件上传总大小 $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $file_upload_count; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'][$index])) { throw new MyRuntimeException(Kohana::lang('o_product.file_upload_error'), 400); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name'][$index]); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_not_flow') . $attach_setup['fileSizePreLimit'], 400); } $file_type_current = FALSE; $file_type_current === FALSE && ($file_type_current = page::getFileType($attach_field, $index)); // 尝试通过Mime类型判断 $file_type_current === FALSE && page::getImageType($_FILES[$attach_field]['tmp_name'][$index]); // 尝试通过图片类型判断 $file_type_current === FALSE && ($file_type_current = page::getPostfix($attach_field, $index)); // 尝试通过后缀截取 //array_walk($attach_setup['allowTypes'], 'strtolower'); foreach ($attach_setup['allowTypes'] as $idx => $item) { $attach_setup['allowTypes'][$idx] = strtolower($item); } if (!empty($attach_setup['allowTypes']) && !in_array(strtolower($file_type_current), $attach_setup['allowTypes'])) { throw new MyRuntimeException(Kohana::lang('o_product.pic_type_error'), 400); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type'][$index]) ? $_FILES[$attach_field]['type'][$index] : ''; // 检测规整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[$index] = array('name' => strip_tags(trim($_FILES[$attach_field]['name'][$index])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmpfile' => $_FILES[$attach_field]['tmp_name'][$index]); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } else { throw new MyRuntimeException(Kohana::lang('o_product.pic_upload_failed'), 400); } } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_count_limit') . $attach_setup['fileCountLimit'], 400); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_product.size') . $file_size_total, 400); } /* // 当前时间戳 $timestamp_current = time(); //预备一些数据 $src_ip_address = $this->input->ip_address(); $attach_meta = array( 'siteId'=>$site_id, 'siteDomain'=>$site_domain, ); // 调用附件服务 //$attachmentService = AttachmentService::get_instance(); require_once(Kohana::find_file('vendor', 'phprpc/phprpc_client',TRUE)); !isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); !isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); */ $route_prefix = Kohana::config('attach.routePrefix'); $route_mask_view = Kohana::config('attach.routeMaskViewProduct'); // 调用productpic服务 $productpicService = ProductpicService::get_instance(); // 遍历所有的上传meta域 foreach ($file_meta_data as $index => $file_meta) { $row_meta_struct = array(); /*$attachment_data_original = array( 'filePostfix'=>$file_meta['type'], 'fileMimeType'=>$file_meta['mime'], 'fileSize'=>$file_meta['size'], 'fileName'=>$file_meta['name'], 'srcIp'=>$src_ip_address, 'attachMeta'=>json_encode($attach_meta), 'createTimestamp'=>$timestamp_current, 'modifyTimestamp'=>$timestamp_current, ); // 调用后端添加附件信息,并调用存储服务存储文件 $args_org = array($attachment_data_original); $sign_org = md5(json_encode($args_org).$phprpcApiKey); $attachment_original_id = $attachmentService->phprpc_addAttachmentFileData($attachment_data_original,@file_get_contents($file_meta['tmpfile']),$sign_org); if (!is_numeric($attachment_original_id)) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 400); }*/ $att = AttService::get_instance($this->img_dir_name); $img_id = $att->save_default_img($file_meta['tmpfile']); if (!$img_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 500); } $productpic_data = array('product_id' => $request_product_id, 'is_default' => ProductpicService::PRODUCTPIC_IS_DEFAULT_FALSE, 'title' => isset($request_pic_title_assoc[$index]) ? strip_tags(trim($request_pic_title_assoc[$index])) : '', 'image_id' => $img_id); $productpic_row_id = $productpicService->add($productpic_data); $return_row_struct = array('id' => $productpic_row_id, 'is_default' => $productpic_data['is_default'], 'title' => $productpic_data['title'], 'image_id' => $img_id, 'picurl_o' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_ORIGINAL, $file_meta['type'], $route_mask_view), 'picurl_t' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_THUMBNAIL, $file_meta['type'], $route_mask_view), 'picurl_l' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_LARGE, $file_meta['type'], $route_mask_view)); $return_data[] = $return_row_struct; // 清理临时文件 @unlink($file_meta['thumb']['tmpfile']); } if (!empty($return_data) && $request_product_id > 0) { //检测是否存在默认图片,没有则设置商品默认图片 if ($productpicService->has_default_pic($request_product_id) == FALSE) { $default_productpic_id = $return_data[0]['id']; $productpicService->set_default_pic_by_productpic_id($default_productpic_id, $request_product_id); $return_data[0]['is_default'] = ProductpicService::PRODUCTPIC_IS_DEFAULT_TRUE; } } //Cache::remove('product_pictures.'.$request_product_id); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '上传成功!'; $return_struct['content'] = $return_data; //$pics = $productpicService->get_stand_pic_by_pic_id($productpic_row_id, ProductpicService::PRODUCTPIC_STANDARDS_COMMON); //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package_name . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ //$this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ //$this->template->content->request_data = $request_data; //* 返回结构体绑定 */ //$this->template->content->return_struct = $return_struct; $this->template->content->picture = $return_struct['content']; } // end of request type determine } catch (MyRuntimeException $ex) { $this->template = new View('layout/default_html'); $this->_ex($ex, $return_struct, $request_data); } }
public function upload() { role::check('product_category'); $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->post(); // 上传的表单域名字 $attach_field = 'category_img'; // 附件应用类型 $attach_app_type = 'productPicAttach'; // 如果有上传请求 if (!page::issetFile($attach_field)) { throw new MyRuntimeException('请选择所要上传的图片', 400); } //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $file_upload_count = page::getFileCount($attach_field); // 初始化一些数据 // 本次文件上传总数量 $file_count_total = 0; // 本次文件上传总大小 $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $file_upload_count; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'][$index])) { throw new MyRuntimeException(Kohana::lang('o_product.file_not_uploaded') . $index, 400); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name'][$index]); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_prelimit') . $attach_setup['fileSizePreLimit'] . Kohana::lang('o_product.index') . $index . Kohana::lang('o_product.size') . $file_size_current, 400); } $file_type_current = FALSE; $file_type_current === FALSE && page::getImageType($_FILES[$attach_field]['tmp_name'][$index]); // 尝试通过图片类型判断 $file_type_current === FALSE && ($file_type_current = page::getFileType($attach_field, $index)); // 尝试通过Mime类型判断 $file_type_current === FALSE && ($file_type_current = page::getPostfix($attach_field, $index)); // 尝试通过后缀截取 if (!empty($attachSetup['allowTypes']) && !in_array($file_type_current, $attach_setup['allowTypes'])) { throw new MyRuntimeException(Kohana::lang('o_product.file_type_invalid') . $index, 400); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type'][$index]) ? $_FILES[$attach_field]['type'][$index] : ''; // 检测规整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[$index] = array('name' => strip_tags(trim($_FILES[$attach_field]['name'][$index])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmpfile' => $_FILES[$attach_field]['tmp_name'][$index]); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_count_limit') . $attach_setup['fileCountLimit'], 400); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_product.size') . $file_size_total, 400); } // 当前时间戳 //$timestamp_current = time(); //预备一些数据 //$src_ip_address = $this->input->ip_address(); //$attach_meta = array (); // 调用附件服务 //$attachmentService = AttachmentService::get_instance(); //require_once (Kohana::find_file('vendor', 'phprpc/phprpc_client', TRUE)); //!isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); //!isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); /*$attachment_data_original = array ( // 'site_id'=>$site_id, 'filePostfix' => $file_meta['type'], 'fileMimeType' => $file_meta['mime'], 'fileSize' => $file_meta['size'], 'fileName' => $file_meta['name'], 'srcIp' => $src_ip_address, 'attachMeta' => json_encode($attach_meta), 'createTimestamp' => $timestamp_current, 'modifyTimestamp' => $timestamp_current ); // 调用后端添加附件信息,并调用存储服务存储文件 $args_org = array ( $attachment_data_original ); $sign_org = md5(json_encode($args_org) . $phprpcApiKey); $attachment_original_id = $attachmentService->phprpc_addAttachmentFileData($attachment_data_original, @file_get_contents($file_meta['tmpfile']), $sign_org); */ $AttService = AttService::get_instance($this->img_dir_name); $file_meta = $file_meta_data[0]; $img_id = $AttService->save_default_img($file_meta['tmpfile']); if (!$img_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 400); } $return_data['pic_attach_id'] = $img_id; $return_data['pic_url'] = $AttService->get_img_url($img_id); // 清理临时文件 @unlink($file_meta['tmpfile']); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '上传成功!'; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ //$this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->title = Kohana::config('site.name'); } // end of request type determine } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data, 'info2', 'layout/default_html'); } }
/** * 上传处理 */ public function uploadForm() { $returnStruct = array('status' => 0, 'code' => 501, 'msg' => _('Not Implemented'), 'content' => array()); try { // 是否调用本地服务 $useLocalService = TRUE; //$useLocalService = FALSE; //* 初始化返回数据 */ $returnStatus = 1; $returnCode = 200; $returnMessage = ''; $returnData = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $requestData = $this->input->post(); //* 实现功能后屏蔽此异常抛出 */ //throw new MyRuntimeException(_('Not Implemented'),501); //* 权限验证,数据验证,逻辑验证 ==根据业务逻辑定制== */ //if(util::isAccess(array(Logon::$MGR_ROLE_LABEL_SYS_ADMIN,), array(Logon::$USER_ROLE_LABEL_DENIED,Logon::$USER_ROLE_LABEL_GUEST), $this->getUserRoleLabel())==FALSE){ // throw new MyRuntimeException(_('Access Denied'),403); //} if (util::isAccess('*', array(Logon::$USER_ROLE_LABEL_DENIED), $this->getUserRoleLabel()) == FALSE) { throw new MyRuntimeException(_('Access Denied'), 403); } //* 权限验证 ==根据业务逻辑定制== */ //* 数据验证 ==根据业务逻辑定制== */ //* 逻辑验证 ==根据业务逻辑定制== */ // 调用底层服务 !isset($servRouteInstance) && ($servRouteInstance = ServRouteInstance::getInstance(ServRouteConfig::getInstance())); // 执行业务逻辑 //:: 多附件上传 // 上传的表单域名字 $attachField = 'myattach'; // 附件应用类型 $attachAppType = 'appAttach'; // 如果有上传请求 if (page::issetFile($attachField)) { $returnData['attach'] = array(); //读取当前应用配置 $attachSetup = Lemon::config('attach.' . $attachAppType); $mimeType2Postfix = Lemon::config('mimemap.type2postfix'); $mimePostfix2Type = Lemon::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $fileUploadCount = page::getFileCount($attachField); // 初始化一些数据 // 本次文件上传总数量 $fileCountTotal = 0; // 本次文件上传总大小 $fileSizeTotal = 0; // 上传文件meta信息 $fileMetaData = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $fileUploadCount; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attachField]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attachField]['tmp_name'][$index])) { throw new MyRuntimeException(_('File not uploaded,index:') . $index, 400); } $fileSizeCurrent = filesize($_FILES[$attachField]['tmp_name'][$index]); if ($attachSetup['fileSizePreLimit'] > 0 && $fileSizeCurrent > $attachSetup['fileSizePreLimit']) { throw new MyRuntimeException(_('File Size PreLimit exceed,Limit:') . $attachSetup['fileSizePreLimit'] . ' index:' . $index . ' size:' . $fileSizeCurrent, 400); } $fileTypeCurrent = FALSE; $fileTypeCurrent === FALSE && page::getImageType($_FILES[$attachField]['tmp_name'][$index]); // 尝试通过图片类型判断 $fileTypeCurrent === FALSE && ($fileTypeCurrent = page::getFileType($attachField, $index)); // 尝试通过Mime类型判断 $fileTypeCurrent === FALSE && ($fileTypeCurrent = page::getPostfix($attachField, $index)); // 尝试通过后缀截取 if (!empty($attachSetup['allowTypes']) && !in_array($fileTypeCurrent, $attachSetup['allowTypes'])) { throw new MyRuntimeException(_('File Type invalid,index:') . $index, 400); } // 当前文件mime类型 $fileMimeCurrent = isset($_FILES[$attachField]['type'][$index]) ? $_FILES[$attachField]['type'][$index] : ''; // 检测规整mime类型 if (!array_key_exists($fileMimeCurrent, $mimeType2Postfix)) { if (array_key_exists($fileTypeCurrent, $mimePostfix2Type)) { $fileMimeCurrent = $mimePostfix2Type[$fileTypeCurrent]; } else { $fileMimeCurrent = 'application/octet-stream'; } } //存储文件meta信息 $fileMetaData[$index] = array('name' => strip_tags($_FILES[$attachField]['name'][$index]), 'size' => $fileSizeCurrent, 'type' => $fileTypeCurrent, 'mime' => $fileMimeCurrent); // 设置上传总数量 $fileCountTotal += 1; // 设置上传总大小 $fileSizeTotal += $fileSizeCurrent; } } if ($attachSetup['fileCountLimit'] > 0 && $fileCountTotal > $attachSetup['fileCountLimit']) { throw new MyRuntimeException(_('File Count Limit exceed,Limit:') . $attachSetup['fileCountLimit'], 400); } if ($attachSetup['fileSizeTotalLimit'] > 0 && $fileSizeTotal > $attachSetup['fileSizeTotalLimit']) { throw new MyRuntimeException(_('File Size Total Limit exceed,Limit:') . $attachSetup['fileSizeTotalLimit'] . ' size:' . $fileSizeTotal, 400); } // 执行上传 // 调用附件服务 if ($useLocalService == TRUE) { !isset($attachmentService) && ($attachmentService = Attachment_Service::getInstance($servRouteInstance)); } else { require_once Lemon::find_file('vendor', 'phprpc/phprpc_client', TRUE); !isset($attachmentService) && ($attachmentService = new PHPRPC_Client(Lemon::config('phprpc.remote.Attachment.host'))); !isset($phprpcApiKey) && ($phprpcApiKey = Lemon::config('phprpc.remote.Attachment.apiKey')); } //预备一些数据 $srcIpAddress = $this->input->ip_address(); $timeStampCurrent = time(); // 遍历所有的上传meta域 foreach ($fileMetaData as $index => $fileMeta) { $attachmentData = array('filePostfix' => $fileMeta['type'], 'fileMimeType' => $fileMeta['mime'], 'fileSize' => $fileMeta['size'], 'fileName' => $fileMeta['name'], 'srcIp' => $srcIpAddress, 'createTimestamp' => $timeStampCurrent, 'modifyTimestamp' => $timeStampCurrent); // 调用后端添加附件信息,并调用存储服务存储文件 if ($useLocalService == TRUE) { $attachmentId = $attachmentService->addAttachmentFileData($attachmentData, @file_get_contents($_FILES[$attachField]['tmp_name'][$index])); } else { $args = array($attachmentData); $sign = md5(json_encode($args) . $phprpcApiKey); $attachmentId = $attachmentService->phprpc_addAttachmentFileData($attachmentData, @file_get_contents($_FILES[$attachField]['tmp_name'][$index]), $sign); } $returnData['attach'][] = $attachmentId; } } //* 补充&修改返回结构体 */ $returnStruct['status'] = $returnStatus; $returnStruct['code'] = $returnCode; $returnStruct['msg'] = $returnMessage; $returnStruct['content'] = $returnData; //* 请求类型 */ if ($this->isAjaxRequest()) { // ajax 请求 // json 输出 $this->template->content = $returnStruct; } else { // html 输出 //* 模板输出 */ $this->template->returnStruct = $returnStruct; $content = new View($this->packageName . '/' . $this->className . '/' . __FUNCTION__); //* 变量绑定 */ $this->template->title = Lemon::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->requestData = $requestData; //* 返回结构体绑定 */ $this->template->content->returnStruct = $returnStruct; //:: 当前应用专用数据 $this->template->content->title = Lemon::config('site.name'); } // end of request type determine } catch (MyRuntimeException $ex) { $returnStruct['status'] = 0; $returnStruct['code'] = $ex->getCode(); $returnStruct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->isAjaxRequest()) { $this->template->content = $returnStruct; } else { $this->template->returnStruct = $returnStruct; $content = new View('info'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->requestData = $requestData; //* 返回结构体绑定 */ $this->template->content->returnStruct = $returnStruct; } } }
public function upload() { role::check('product_attribute'); try { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $return_data = $request_data = $this->input->post(); // 上传的表单域名字 $attach_field = 'attribute_img'; // 附件应用类型 $attach_app_type = 'productPicAttach'; // 如果无上传请求 if (!page::issetFile($attach_field)) { throw new MyRuntimeException('请选择需要上传的图片', 400); } $title = isset($request_data['attribute_img_title']) && is_array($request_data['attribute_img_title']) && !empty($request_data['attribute_img_title']) ? $request_data['attribute_img_title'] : array(); //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $file_upload_count = page::getFileCount($attach_field); // 初始化一些数据 // 本次文件上传总数量 $file_count_total = 0; // 本次文件上传总大小 $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $file_upload_count; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'][$index])) { throw new MyRuntimeException(Kohana::lang('o_product.file_not_uploaded') . $index, 400); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name'][$index]); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_prelimit') . $attach_setup['fileSizePreLimit'] . Kohana::lang('o_product.index') . $index . Kohana::lang('o_product.size') . $file_size_current, 400); } $file_type_current = FALSE; $file_type_current === FALSE && page::getImageType($_FILES[$attach_field]['tmp_name'][$index]); // 尝试通过图片类型判断 $file_type_current === FALSE && ($file_type_current = page::getFileType($attach_field, $index)); // 尝试通过Mime类型判断 $file_type_current === FALSE && ($file_type_current = page::getPostfix($attach_field, $index)); // 尝试通过后缀截取 if (isset($attach_setup['allowTypes']) && !in_array($file_type_current, $attach_setup['allowTypes'])) { throw new MyRuntimeException(Kohana::lang('o_product.file_type_invalid') . $_FILES[$attach_field]['name'][$index], 400); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type'][$index]) ? $_FILES[$attach_field]['type'][$index] : ''; // 检测规整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[$index] = array('name' => strip_tags(trim($_FILES[$attach_field]['name'][$index])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmbfile' => $_FILES[$attach_field]['tmp_name'][$index]); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_count_limit') . $attach_setup['fileCountLimit'], 400); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_product.size') . $file_size_total, 400); } // 当前时间戳 //$timestamp_current = time(); //预备一些数据 //$src_ip_address = $this->input->ip_address(); //$attach_meta = array (); //require_once (Kohana::find_file('vendor', 'phprpc/phprpc_client', TRUE)); //!isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); //!isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); // 调用附件存储服务 for ($index = 0; $index < $file_count_total; $index++) { $img_id = AttService::get_instance()->save_default_img($file_meta_data[$index]['tmbfile']); if (!$img_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 400); } $attribute_image = array('attach_id' => $img_id, 'title' => isset($title[$index]) ? strip_tags(trim($title[$index])) : ''); $return_data['picurl'] = $attribute_image['picurl'] = AttService::get_instance()->get_img_url($img_id); $return_data['meta'] = implode('|', $attribute_image); // 清理临时文件 @unlink($file_meta_data[$index]['tmbfile']); } //echo "<pre>";print_r($file_meta_data);print_r($return_data);die(); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '上传成功!'; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package_name . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ //$this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->title = Kohana::config('site.name'); } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { //$return_struct['action']['type']= 'close'; // 当前应用为弹出窗口所以定义失败后续动作为关闭窗口 $this->template = new View('layout/default_html'); $this->template->return_struct = $return_struct; $content = new View('info2'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; $return_struct['action'] = array('type' => 'location', 'url' => request::referrer()); //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }