123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- include "Uploader.class.php";
- /* 上传配置 */
- $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;
- }
- /* 生成上传实例对象并完成上传 */
- $up = new Uploader($fieldName, $config, $base64);
- /**
- * 得到上传文件所对应的各个参数,数组结构
- * array(
- * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
- * "url" => "", //返回的地址
- * "title" => "", //新文件名
- * "original" => "", //原始文件名
- * "type" => "" //文件类型
- * "size" => "", //文件大小
- * )
- */
- $result = $up->getFileInfo();
- $fileUrl = $result['url'];
- $filePath = ltrim(str_replace(UPLOAD_URL,'',$fileUrl),'/');//文件相对路径
- $targetFile = str_replace('/',DIRECTORY_SEPARATOR,UPLOAD_PATH.$filePath);
- $filesize = filesize($targetFile);
- //上传设置(判断是否采用全路径)
- $attConfigInfo = \app\modules\admin\models\Config::find()->where("name='attachment'")->one();
- $attConfig = string2array($attConfigInfo->value);
- if(Yii::$app->params['oss']['OPEN_OSS']==1)//如果开启了存储
- {
- $oss = new \app\common\components\Oss();
- $initResult = $oss->init();
- ob_clean();
- if($initResult['error']==0)
- {
- $ossResult = $oss->Upload($targetFile,$filePath);
- if($ossResult['error']==0)
- {
- $fileUrl = $ossResult['data']['url'];
- $filePath = $attConfig['absolute_url']?$ossResult['data']['url']:$ossResult['data']['path'];
- @unlink($targetFile);
- $result['url'] = getFileUrl($filePath);
- }
- else
- {
- return json_encode([
- 'code' => 1,
- 'msg' => $ossResult['msg']
- ]);
- }
- }
- else
- {
- return json_encode([
- 'code' => 1,
- 'msg' => $initResult['msg']
- ]);
- }
- }
- else
- {
- $filePath = $attConfig['absolute_url']?$fileUrl:$filePath;
- }
- //写入附件表
- $attachment = new \app\modules\admin\models\Attachment();
- $attachment->file_name = basename($filePath);
- $attachment->file_path = $filePath;
- $attachment->file_size = $filesize;
- $attachment->file_ext = fileext(basename($filePath));
- $attachment->hash = md5($filePath);
- $attachment->upload_time = time();
- $attachment->upload_ip = ip();
- $attachment->status = 1;
- $attachment->list_order = 0;
- $attachment->folder = Yii::$app->request->post('folder','');
- $attachment->save();
- return json_encode($result);
|