PHP code example of yii2tech / ar-file

1. Go to this page and download the library: Download yii2tech/ar-file 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/ */

    

yii2tech / ar-file example snippets


return [
    'components' => [
        'fileStorage' => [
            'class' => 'yii2tech\filestorage\local\Storage',
            'basePath' => '@webroot/files',
            'baseUrl' => '@web/files',
            'filePermission' => 0777,
            'buckets' => [
                'item' => [
                    'baseSubPath' => 'item',
                ],
            ]
        ],
        // ...
    ],
    // ...
];

use yii2tech\ar\file\FileBehavior;

class Item extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'file' => [
                'class' => FileBehavior::className(),
                'fileStorageBucket' => 'item',
                'fileExtensionAttribute' => 'fileExtension',
                'fileVersionAttribute' => 'fileVersion',
            ],
        ];
    }
    // ...
}

$model = Item::findOne(1);
$model->saveFile('/path/to/source/file.dat');

$model = Item::findOne(1);
$model->deleteFile();

$model = Item::findOne(1);
if ($model->fileExists()) {
    echo $model->getFileUrl(); // outputs file URL
    echo $model->getFileContent(); // outputs file content
} else {
    echo 'No file attached';
}

use yii\web\UploadedFile;

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

var_dump($model->fileExists()); // outputs `true`

class Item extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            // ...
            ['file', 'file', 'mimeTypes' => ['image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'], 'skipOnEmpty' => !$this->isNewRecord],
        ];
    }
    // ...
}


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

/* @var $model Item */

use yii\web\Controller;

class ItemController extends Controller
{
    public function actionCreate()
    {
        $model = new Item();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    // ...
}

use yii2tech\ar\file\TransformFileBehavior;
use yii\imagine\Image;

class Item extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'file' => [
                'class' => TransformFileBehavior::className(),
                'fileStorageBucket' => 'item',
                'fileExtensionAttribute' => 'fileExtension',
                'fileVersionAttribute' => 'fileVersion',
                'transformCallback' => function ($sourceFileName, $destinationFileName, $options) {
                    try {
                        Image::thumbnail($sourceFileName, $options['width'], $options['height'])->save($destinationFileName);
                        return true;
                    } catch (\Exception $e) {
                        return false;
                    }
                },
                'fileTransformations' => [
                    'origin', // no transformation
                    'main' => [
                        'width' => 400,
                        'height' => 400,
                    ],
                    'thumbnail' => [
                        'width' => 100,
                        'height' => 100,
                    ],
                ],
            ],
        ];
    }
    // ...
}

$model = Item::findOne(1);
echo $model->getFileUrl('origin'); // outputs URL for the full-sized image
echo $model->getFileUrl('main'); // outputs URL for the medium-sized image
echo $model->getFileUrl('thumbnail'); // outputs URL for the thumbnail image

use yii2tech\ar\file\TransformFileBehavior;
use yii\imagine\Image;

class Item extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'file' => [
                'class' => TransformFileBehavior::className(),
                'fileTransformations' => [
                    'origin', // no transformation
                    'preview' => [
                        // ...
                    ],
                    'web' => [
                        // ...
                    ],
                ],
                'transformationFileExtensions' => [
                    'preview' => 'jpg',
                    'web' => function ($fileExtension) {
                        return in_array($fileExtension, ['jpg', 'jpeg', 'png', 'gif']) ? $fileExtension : 'jpg';
                    },
                ],
                // ...
            ],
        ];
    }
    // ...
}

$model = Item::findOne(1);
$model->regenerateFileTransformations('origin'); // regenerate transformations using 'origin' as a source

use yii2tech\ar\file\ImageFileBehavior;

class Item extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'file' => [
                'class' => ImageFileBehavior::className(),
                'fileStorageBucket' => 'item',
                'fileExtensionAttribute' => 'fileExtension',
                'fileVersionAttribute' => 'fileVersion',
                'fileTransformations' => [
                    'origin', // no resize
                    'main' => [800, 600], // width = 800px, height = 600px
                    'thumbnail' => [100, 80], // width = 100px, height = 80px
                ],
            ],
        ];
    }
    // ...
}

php composer.phar