fgh151/yii2-s3-upload

Yii2 S3 upload extension

Installs: 1 138

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

0.0.3 2025-02-11 14:00 UTC

This package is auto-updated.

Last update: 2025-05-23 08:31:57 UTC


README

S3 upload extension

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist fgh151/yii2-s3-upload "*"

or add

"fgh151/yii2-s3-upload": "*"

to the require section of your composer.json file.

Usage

Add component to config file :

<?= 
'components' => [
    'storage' => [
        'class' => fgh151\yii2\s3upload\S3Storage::class,
        'key' => 's3-api-key',
        'secret' => 's3-api-secret',
        'bucket' => 'bucket-name'
        //You may also change region, provider, etc
    ],
] ?>

Your form model:

class FormModel extends \yii\db\ActiveRecord
{
    public $uploadImage;
    public $pathToImage;

    public function rules()
    {
        return [
            ['uploadImage', 'file', 'extensions' => ['png', 'jpg', 'jpeg']],
        ];
    }
    public function behaviors()
    {
        return [
            [
                'class' => fgh151\yii2\s3upload\S3UploadBehavior::class, //Behavior class
                'attribute' => 'uploadImage',
                'storageAttribute' => 'pathToImage', //Entity indefier in mapping clas
            ],
        ];
    }
    
    public function afterSave($insert,$changedAttributes){
        parent::afterSave($insert,$changedAttributes);
        if ($this->pathToImage !== null) {
            //TODO: save $this->pathToImage
        }
    }
}

Form field example:

<?= $form->field($model, 'uploadImage')->fileInput() ?>

For use with dynamic forms like yii2-dynamicform, you dont need set behaviour in model function. But in controller, when create models, must set:

$images = Model::createMultiple(StoreImage::class, $images);

foreach ($images as $i => $image) {
    $image->attachBehavior('upload', [
        'class' => S3UploadBehavior::class, //Behavior class
        'attribute' => "[{$i}]file_upload", // Because input name is different
        'storageAttribute' => 'file', //Entity indefier in mapping clas
        'path' => 'product', //Entity indefier in mapping clas
    ]);
}