PHP code example of mheads / yii2-filestorage

1. Go to this page and download the library: Download mheads/yii2-filestorage 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/ */

    

mheads / yii2-filestorage example snippets


'controllerMap' => [
    // ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => null,
        'migrationNamespaces' => [
            // ...
            'mheads\filestorage\migrations',
        ],
    ],
],

const MHEADS_FILE_STORAGE_COMPONENT_NAME = 'fileStorage';
 
$config = [
    //...
    'components' => [
        //...
        MHEADS_FILE_STORAGE_COMPONENT_NAME => [
            'class' => \mheads\filestorage\Storage::class,
            'stores'           => [
                'upload' => [
                    'class'           => \mheads\filestorage\stores\fileSystem\FileSystemStore::class,
                    'baseUrl'         => '/upload',
                    'basePath'        => __DIR__.'/../../upload',
                    'basePrivatePath' => __DIR__.'/../../private_upload',
                    'isHttps'         => true,
                    'host'            => 'static.example.com',
                ],
            ],
            'defaultStoreName' => 'upload',
            'strictRemove'     => false,
        ],
    ]
];

$uploadedFile = \yii\web\UploadedFile::getInstanceByName('picture');
$file = \mheads\filestorage\File::create(
	$uploadedFile,
	'product-pictures',
);
$file->add();

use mheads\filestorage\File;

$file = File::find()->where([File::field_id => 123])->one();

$url = $file->getUrl();
$content = $file->getContent();

use mheads\filestorage\File;

$file = File::find()->where([File::field_id => 123])->one();
$file->remove();

use mheads\filestorage\File;
use mheads\filestorage\behaviors\FileUploadBehavior;
use \yii\web\UploadedFile;

/**
 * @property ?int $id
 * @property ?string $name
 * @property ?UploadedFile|int $picture_id
 */
class Product extends \yii\db\ActiveRecord
{
    //......
    
    public function setPicture(?UploadedFile $file): void
    {
        $this->setAttribute('picture_id', $file);
    }
    
    public function getPicture()
    {
        return $this->hasOne(File::class, [File::field_id => 'picture_id']);
    }
    
    public function behaviors(): array
    {
        return [
            'pictureUpload' => [
                'class'     => FileUploadBehavior::class,
                'attribute' => 'picture_id',
                'isPrivate' => false,
                'groupName' => 'product-pictures',
            ],
        ];
    }
    
    //......
}

const MHEADS_FILE_STORAGE_COMPONENT_NAME = 'fileStorage';
 
$config = [
    //...
    'components' => [
        //...
        MHEADS_FILE_STORAGE_COMPONENT_NAME => [
            'class' => \mheads\filestorage\Storage::class,
            'stores'           => [
                'upload' => [
                    'class'           => \mheads\filestorage\stores\fileSystem\FileSystemStore::class,
                    'baseUrl'         => '/upload',
                    'basePath'        => __DIR__.'/../../upload',
                    'basePrivatePath' => __DIR__.'/../../private_upload',
                    'isHttps'         => true,
                    'host'            => 'static.example.com',
                ],
                'upload2' => [
                    'class'           => \mheads\filestorage\stores\fileSystem\FileSystemStore::class,
                    'baseUrl'         => '/upload2',
                    'basePath'        => '/media/store/data/upload',
                    'basePrivatePath' => '/media/store/data/private_upload',
                    'isHttps'         => true,
                    'host'            => 'static2.example.com',
                ],
            ],
            'defaultStoreName' => 'upload',
            'strictRemove'     => false,
        ],
    ],
];

$uploadedFile = \yii\web\UploadedFile::getInstanceByName('picture');
$file = \mheads\filestorage\File::create(
	$uploadedFile,
	'product-pictures',
	'upload2', // store name
);
$file->add();

public function behaviors(): array
{
    return [
        'pictureUpload' => [
            'class'     => FileUploadBehavior::class,
            'attribute' => 'picture_id',
            'isPrivate' => false,
            'groupName' => 'product-pictures',
            'storeName' => 'upload2', // store name
        ],
    ];
}