eluhr / yii2-fileflyupload
Filefly upload for yii2 models
Installs: 1 668
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- dmstr/yii2-filefly-module: ^1.4
- yiisoft/yii2: ^2.0
This package is auto-updated.
Last update: 2024-10-24 22:51:16 UTC
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