Upload.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\common\components;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\web\UploadedFile;
  6. use yii\helpers\FileHelper;
  7. use app\modules\admin\models\Attachment;
  8. /**
  9. * 文件上传处理
  10. */
  11. class Upload extends Model
  12. {
  13. public $file;
  14. private $_appendRules;
  15. public function init ()
  16. {
  17. parent::init();
  18. if(!empty(Yii::$app->params['webuploader']['baseConfig']['accept']['extensions']))
  19. {
  20. $extensions = Yii::$app->params['webuploader']['baseConfig']['accept']['extensions'];
  21. }
  22. else
  23. {
  24. $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';
  25. }
  26. $this->_appendRules = [
  27. [['file'], 'file', 'extensions' => $extensions],
  28. ];
  29. }
  30. public function rules()
  31. {
  32. $baseRules = [];
  33. return array_merge($baseRules, $this->_appendRules);
  34. }
  35. /**
  36. *
  37. */
  38. public function upImage ()
  39. {
  40. $model = new static;
  41. $model->file = UploadedFile::getInstanceByName('file');
  42. if (!$model->file) {
  43. return false;
  44. }
  45. $targetPath = '';
  46. if ($model->validate()) {
  47. $childPath = date('Y',TIMESTAMP).DIRECTORY_SEPARATOR.date('m',TIMESTAMP).DIRECTORY_SEPARATOR.date('d',TIMESTAMP).DIRECTORY_SEPARATOR;
  48. $targetPath = UPLOAD_PATH.$childPath;
  49. $fileName = get_unique_file_name($targetPath,$model->file->extension);//文件重命名
  50. if (!is_dir($targetPath)) {
  51. FileHelper::createDirectory($targetPath);
  52. }
  53. $targetFile = $targetPath . $fileName;
  54. $model->file->saveAs($targetFile);
  55. $params = Yii::$app->params;
  56. //上传设置(判断是否采用全路径)
  57. $attConfigInfo = \app\modules\admin\models\Config::find()->where("name='attachment'")->one();
  58. $attConfig = string2array($attConfigInfo->value);
  59. $ossConfigResult = \app\modules\admin\models\Config::find()->where("name='oss'")->one();
  60. $ossConfig = string2array($ossConfigResult['value']);
  61. $filePath = str_replace(DIRECTORY_SEPARATOR,'/',$childPath.$fileName);
  62. $fileUrl = UPLOAD_URL.$filePath;
  63. if($params['oss']['OPEN_OSS']==1)//如果开启了云存储,初始化存储对象
  64. {
  65. $oss = new \app\common\components\Oss();
  66. $initResult = $oss->init($ossConfig['OPEN_INTERNAL']);
  67. if($initResult['error']==0)
  68. {
  69. $result = $oss->Upload($targetFile,$filePath);
  70. if($result['error']==0)
  71. {
  72. $fileUrl = $result['data']['url'];
  73. $filePath = $attConfig['absolute_url']?$result['data']['url']:$result['data']['path'];
  74. @unlink($targetFile);
  75. }
  76. else
  77. {
  78. return [
  79. 'code' => 1,
  80. 'msg' => $result['msg']
  81. ];
  82. }
  83. }
  84. else
  85. {
  86. return [
  87. 'code' => 1,
  88. 'msg' => $initResult['msg']
  89. ];
  90. }
  91. }
  92. else
  93. {
  94. $filePath = $attConfig['absolute_url']?$fileUrl:$filePath;
  95. }
  96. //写入附件表
  97. $attachment = new Attachment();
  98. $attachment->file_name = $model->file->name;
  99. $attachment->file_path = $filePath;
  100. $attachment->file_size = $model->file->size;
  101. $attachment->file_ext = $model->file->extension;
  102. $attachment->hash = md5($filePath);
  103. $attachment->upload_time = TIMESTAMP;
  104. $attachment->upload_ip = MYIP;
  105. if(!empty($_POST['md5'])){$attachment->md5 = safe_replace($_POST['md5']);}
  106. $attachment->status = 1;
  107. $attachment->list_order = 0;
  108. $attachment->folder = Yii::$app->request->post('folder','');
  109. $attachment->save();
  110. return [
  111. 'code' => 0,
  112. 'url' =>$fileUrl,
  113. 'attachment' => $filePath,
  114. 'name'=>$model->file->name,
  115. 'size'=>$model->file->size
  116. ];
  117. } else {
  118. $errorsInfo = $model->getFirstErrors();
  119. if(is_array($errorsInfo))foreach($errorsInfo as $k=>$v)
  120. {
  121. $errors[] = $v;
  122. }
  123. return [
  124. 'code' => 1,
  125. 'msg' => $errors[0]
  126. ];
  127. }
  128. }
  129. }