lav45/yii2-file-upload-module

This is Yii2 extension for the upload files

Installs: 1 042

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 3

Forks: 4

Type:yii2-extension

1.0.2 2024-02-20 14:41 UTC

This package is auto-updated.

Last update: 2024-03-29 10:34:00 UTC


README

This is a module for the Yii2 Framework which will help you upload files and access them from the browser.

Installation

The preferred way to install this extension is through composer.

Either run

~$ composer require --prefer-dist lav45/yii2-file-upload-module

or add

"lav45/yii2-file-upload-module": "1.0.*"

to the require section of your composer.json file.

Basic Usage:

Add path aliases and url to your file store in the main config You need to configure your web server to the @storageDir directory and specify @storageUrl

return [
    'aliases' => [
        '@storageUrl' => 'https://cdn.site.com/storage',
    ],
    'components' => [
        'fs' => [
            'class' => creocoder\flysystem\LocalFilesystem::className(),
            'path' => '@common/cdn',
        ]
    ],
];

Add action to the main controller

use lav45\fileUpload\UploadAction;

class PageController extends Controller
{
    public function actions()
    {
        return [
            'upload' => [
                'class' => UploadAction::className(),
            ],
        ];
    }
}

Need to add to your ActiveRecord model

use lav45\fileUpload\UploadTrait;
use lav45\fileUpload\UploadBehavior;
use lav45\fileUpload\UploadInterface;

class Page extends ActiveRecord implements UploadInterface
{
    use UploadTrait;

    public function rules()
    {
        return [
            [['image'], 'string'],
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => UploadBehavior::className(),
                'attribute' => 'image',
            ],
        ];
    }
    
    public function getUploadPath()
    {
        return '/page/' . $this->id;
    }
}

Need to add a field for uploading files

/**
 * @var Page $model
 */
 
use lav45\fileUpload\widget\FileUpload;

$form = ActiveForm::begin();

echo $form->field($model, 'image')->widget(FileUpload::className());

ActiveForm::end();

Displays the uploaded file

<?php
/**
 * @var Page $model
 */
 ?>
 
<img src="<?= $model->getAttributeUrl('image') ?>" alt="">