PHP code example of paulzi / yii2-file-behavior

1. Go to this page and download the library: Download paulzi/yii2-file-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

paulzi / yii2-file-behavior example snippets


class Sample extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'path'  => '@webroot/files',
                'url'   => '@web/files',
                'attributes' => [
                    'file'  => [],
                    'files' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                    ],
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'types' => [
                            'original' => [1200, 1200],
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                    'images' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                        'item'  => [
                            'class' => 'paulzi\fileBehavior\Image',
                            'types' => [
                                'thm'  => [120, 120],
                            ],
                        ]
                    ],
                ],
            ],
        ];
    }
}

$model = Sample::findOne(1);
$file  = UploadedFile::getInstance($model, 'file');
$model->file->value = $file->tempName;
$model->save();

$model = Sample::findOne(2);
$files = UploadedFile::getInstances($model, 'images');
foreach ($files as $file) {
    $model->images[] = $file->tempName;
}
$model->save();

$model = Sample::findOne(1);
$url   = $model->file->url;
$path  = $model->file->path;

$model = Sample::findOne(2);
foreach ($model->images as $image) {
    echo $image->url;      // original image url
    echo $image->thm->url; // thm image url
}

$model = Sample::findOne(1);
$model->file->value = null;
$model->save();

$model = Sample::findOne(1);
$model->files[2]->value = null;
$model->save();

$model = Sample::findOne(2);
$model->images->value = null;
$model->save();

    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'attributes' => [
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'salt'  => 'secret',
                        'types' => [
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                ],
            ],
        ];
    }

    'aliases' => [
        '@cdnWeb' => 'http://s.example.com',
    ],

    'on beforeRequest' => function () {
        \Yii::$container->set('paulzi\fileBehavior\FileBehavior', [
            'path' => '@cdn\web\files',
            'url'  => '@cdnWeb\web\files',
        ]);
        \Yii::$container->set('paulzi\fileBehavior\Image', [
            'salt' => Yii::$app->params['salt'],
        ]);
    },

    'salt' => 'secret salt',