123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\common\components;
- use Yii;
- use yii\base\Model;
- use yii\web\UploadedFile;
- use yii\helpers\FileHelper;
- use app\modules\admin\models\Attachment;
- /**
- * 文件上传处理
- */
- class Upload extends Model
- {
- public $file;
- private $_appendRules;
- public function init ()
- {
- parent::init();
- if(!empty(Yii::$app->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]
- ];
- }
- }
- }
|