eluhr/yii2-fileflyupload 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eluhr/yii2-fileflyupload

Composer 安装命令:

composer require eluhr/yii2-fileflyupload

包简介

Filefly upload for yii2 models

README 文档

README

A little helper trait to easily upload files from a local filesystem to another filesystem

Installation

composer require eluhr/yii2-fileflyupload

Usage

Your config must configure the needed filesystems see creocoder/yii2-flysystem

Use the trait in your Model

 namespace example\namespace\models;

 use eluhr\fileflyupload\traits\FileflyUploadTrait;
 use yii\base\Model;
 use yii\helpers\FileHelper;

 class MyModel extends Model
 {
     use FileflyUploadTrait;

     public $file;
     
     public function getLocalFs(): string {
         return 'fsLocal';
     }
     
     public function getStorageFs(): string {
         return 'fsStorage';
     }

     public function rules(): array
     {
         $rules = parent::rules();
         $rules[] = [
             'file',
             'file',
             'skipOnEmpty' => false,
             'extensions' => 'pdf',
             'maxSize' => 3145728 // 3 MB
         ];
         return $rules;
     }

     public function upload(): bool
     {
         if ($this->validate()) {
             $relativePath = '/path/to/file.pdf';
             $absolutePath = \Yii::$app->get($this->getLocalFs())->path . $relativePath;
             if (!FileHelper::createDirectory(dirname($absolutePath))) {
                 return false;
             }
             if ($this->file->saveAs($absolutePath) && $this->moveLocalFileToStorage($relativePath)) {
                 return true;
             }
             $this->addError('file', \Yii::t('model','Error while uploading file'));
         }
         return false;
     }
     
           
     public function beforeDelete()
     {
         if (!$this->deleteFromStorage('/path/to/file.pdf')) {
             return false;
         }
         return parent::beforeDelete();
     }
 }

In your controller you just call the method moveLocalFileToStorage

 namespace example\namespace\controllers;

 use my\namespace\models\MyModel;
 use yii\web\Controller;
 use yii\web\UploadedFile;
 use Yii;

 class MyController extends Controller
 {
     public function actionUpload()
     {
         $model = new MyModel();
         if (Yii::$app->getRequest()->getIsPost()) {
             $model->file = UploadedFile::getInstance($model, 'file');
             if ($model->upload()) {
                 return $this->redirect(['upload']);
             }
         }
         return $this->render('upload', ['model' => $model]);
     }
 }

Your view file can look something like this

use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin();
echo $form->field($model, 'file')->fileInput(['accept' => 'application/pdf']);
echo Html::submitButton();
ActiveForm::end();

Note: This is a very basic example. This may not suit your needs. Please do not blindly copy and paste it

统计信息

  • 总下载量: 1.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2022-02-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固