params['webuploader']['baseConfig']['accept']['extensions'])) { $extensions = Yii::$app->params['webuploader']['baseConfig']['accept']['extensions']; } else { $extensions = 'ico,gif,jpg,jpeg,bmp,png,pdf,doc,docx,ppt,pptx,xls,xlsx,rar,zip,7z,tar.gz,war,txt,mp4,mp3,flv,wps,et,dps'; } $this->_appendRules = [ [['file'], 'file', 'extensions' => $extensions], ]; } public function rules() { $baseRules = []; return array_merge($baseRules, $this->_appendRules); } /** * */ public function upImage () { $model = new static; $model->file = UploadedFile::getInstanceByName('file'); if (!$model->file) { return false; } $targetPath = ''; if ($model->validate()) { $childPath = date('Y',TIMESTAMP).DIRECTORY_SEPARATOR.date('m',TIMESTAMP).DIRECTORY_SEPARATOR.date('d',TIMESTAMP).DIRECTORY_SEPARATOR; $targetPath = UPLOAD_PATH.$childPath; $fileName = get_unique_file_name($targetPath,$model->file->extension);//文件重命名 if (!is_dir($targetPath)) { FileHelper::createDirectory($targetPath); } $targetFile = $targetPath . $fileName; $model->file->saveAs($targetFile); $params = Yii::$app->params; //上传设置(判断是否采用全路径) $attConfigInfo = \app\modules\admin\models\Config::find()->where("name='attachment'")->one(); $attConfig = string2array($attConfigInfo->value); $ossConfigResult = \app\modules\admin\models\Config::find()->where("name='oss'")->one(); $ossConfig = string2array($ossConfigResult['value']); $filePath = str_replace(DIRECTORY_SEPARATOR,'/',$childPath.$fileName); $fileUrl = UPLOAD_URL.$filePath; if($params['oss']['OPEN_OSS']==1)//如果开启了云存储,初始化存储对象 { $oss = new \app\common\components\Oss(); $initResult = $oss->init($ossConfig['OPEN_INTERNAL']); if($initResult['error']==0) { $result = $oss->Upload($targetFile,$filePath); if($result['error']==0) { $fileUrl = $result['data']['url']; $filePath = $attConfig['absolute_url']?$result['data']['url']:$result['data']['path']; @unlink($targetFile); } else { return [ 'code' => 1, 'msg' => $result['msg'] ]; } } else { return [ 'code' => 1, 'msg' => $initResult['msg'] ]; } } else { $filePath = $attConfig['absolute_url']?$fileUrl:$filePath; } //写入附件表 $attachment = new Attachment(); $attachment->file_name = $model->file->name; $attachment->file_path = $filePath; $attachment->file_size = $model->file->size; $attachment->file_ext = $model->file->extension; $attachment->hash = md5($filePath); $attachment->upload_time = TIMESTAMP; $attachment->upload_ip = MYIP; if(!empty($_POST['md5'])){$attachment->md5 = safe_replace($_POST['md5']);} $attachment->status = 1; $attachment->list_order = 0; $attachment->folder = Yii::$app->request->post('folder',''); $attachment->save(); return [ 'code' => 0, 'url' =>$fileUrl, 'attachment' => $filePath, 'name'=>$model->file->name, 'size'=>$model->file->size ]; } else { $errorsInfo = $model->getFirstErrors(); if(is_array($errorsInfo))foreach($errorsInfo as $k=>$v) { $errors[] = $v; } return [ 'code' => 1, 'msg' => $errors[0] ]; } } }