public function upload()
 {
     try {
         $config = C('upload');
         $upload = new \Think\Upload($config);
         $info = $upload->upload();
         $errmsg = null;
         if (!$info) {
             $errmsg = $upload->getError();
             throw new \Exception($this->errcode . ':' . $errmsg);
             // return get_api_result('component', array(), $this->errcode.':'.$errmsg);
         }
         $fileinfo = array();
         $attData = array();
         // 存储文件信息
         foreach ($info as $val) {
             $fileinfo['atname'] = $val['savename'];
             $fileinfo['atpath'] = $val['savepath'];
             $fileinfo['atmine'] = $val['type'];
             $fileinfo['atext'] = $val['ext'];
             $fileinfo['created'] = time();
             if (in_array($val['ext'], $this->imgExts)) {
                 $fileinfo['attype'] = 1;
             }
             $attData[] = $fileinfo;
         }
         $attModel = D('Attachment');
         $rs = $attModel->data($attData)->addAll($attData);
         if (!$rs) {
             throw new \Exception(L('_ERR_UPLOAD_'));
         }
         $attInfo = array();
         $attInfo['aid'] = $rs;
         $attInfo['att_url'] = set_attr_url($rs);
         return get_api_result('component', $attInfo);
     } catch (\Exception $e) {
         $errInfo = $e->getMessage();
         return get_api_result('component', array(), $errInfo);
     }
 }
예제 #2
0
 public function action_upload($CONFIG)
 {
     /* 上传配置 */
     $base64 = "upload";
     switch (htmlspecialchars($_GET['action'])) {
         case 'uploadimage':
             $config = array("pathFormat" => $CONFIG['imagePathFormat'], "maxSize" => $CONFIG['imageMaxSize'], "allowFiles" => $CONFIG['imageAllowFiles']);
             $fieldName = $CONFIG['imageFieldName'];
             break;
         case 'uploadscrawl':
             $config = array("pathFormat" => $CONFIG['scrawlPathFormat'], "maxSize" => $CONFIG['scrawlMaxSize'], "allowFiles" => $CONFIG['scrawlAllowFiles'], "oriName" => "scrawl.png");
             $fieldName = $CONFIG['scrawlFieldName'];
             $base64 = "base64";
             break;
         case 'uploadvideo':
             $config = array("pathFormat" => $CONFIG['videoPathFormat'], "maxSize" => $CONFIG['videoMaxSize'], "allowFiles" => $CONFIG['videoAllowFiles']);
             $fieldName = $CONFIG['videoFieldName'];
             break;
         case 'uploadfile':
         default:
             $config = array("pathFormat" => $CONFIG['filePathFormat'], "maxSize" => $CONFIG['fileMaxSize'], "allowFiles" => $CONFIG['fileAllowFiles']);
             $fieldName = $CONFIG['fileFieldName'];
             break;
     }
     /* 生成上传实例对象并完成上传 */
     $config['pathFormat'] = __ROOT__ . $config['pathFormat'];
     $up = new \Component\Common\UUploader($fieldName, $config, $base64);
     /**
      * 得到上传文件所对应的各个参数,数组结构
      * array(
      *     "state" => "",          //上传状态,上传成功时必须返回"SUCCESS"
      *     "url" => "",            //返回的地址
      *     "title" => "",          //新文件名
      *     "original" => "",       //原始文件名
      *     "type" => ""            //文件类型
      *     "size" => "",           //文件大小
      * )
      */
     $attInfo = $up->getFileInfo();
     // var_dump(file_exists($attInfo['url']),$attInfo);
     if ($attInfo['url']) {
         $fileinfo['atname'] = $attInfo['title'];
         $fileinfo['atmine'] = rmime_content_type($attInfo['title']);
         $fileinfo['atext'] = substr($attInfo['title'], strrpos($attInfo['title'], '.') + 1);
         if (in_array($fileinfo['atext'], C('imgexts'))) {
             $fileinfo['attype'] = 1;
         }
         $fileinfo['created'] = time();
         // 求出文件的保存路径
         $tmpStr = strchr($attInfo['url'], '/ueditor/');
         $tmpStr = ltrim(substr($tmpStr, 0, strripos($tmpStr, '/') + 1), '/');
         $fileinfo['atpath'] = $tmpStr;
         $attModel = D('Attachment');
         $rs = $attModel->data($fileinfo)->add();
         if (!$rs) {
             $attInfo['url'] = '';
         }
         //var_dump(111);
         $attInfo['url'] = set_attr_url($rs);
     }
     /* 返回数据 */
     return json_encode($attInfo);
 }