PHP code example of sem-soft / yii2-filestorage

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

    

sem-soft / yii2-filestorage example snippets



...
  'components'  =>  [
    ...
    'filestorage'	=>  [
        'class'             => \sem\filestorage\FileStorage::className(),
        'storageBaseUrl'    =>  false,
        'storagePath'       =>  '@webroot',
        'storageDir'        =>  'upload',
        'filemode'          =>  0775 // Если задан, то после создания файла принудительно будет произведена смена прав на указанные
    ]
    ...
  ],
...
 

    public function actionIndex()
    {
	$model = new \backend\models\FileForm();
	
	if (Yii::$app->request->isPost) {
	    
            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
	    
            $file = $model->upload();
	    
        }
	
        return $this->render('index', [
	    'model'	=>  $model
	]);
    }
 

     public function actionIndex1()
    {
        $model = new \backend\models\FileForm();


            $model->imageFile = new \sem\filestorage\adapters\RemoteFile("https://cs7065.userapi.com/c836722/v836722161/4bff2/mdg7cPZvLrM.jpg");

            $file = $model->upload();

        return $this->render('index', [
                'model' => $model
        ]);
    }
 



namespace backend\models;

class FileForm extends \yii\base\Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
        ];
    }
    
    /**
     * @return boolean
     */
    public function upload()
    {
        if ($this->validate()) {
	    $file = new \sem\filestorage\models\File($this->imageFile,[
		'group_code'	=>  'banners',
		'object_id'	=>  '345',
		'allowedExtensions' =>	[
		    'png',
		    'jpeg',
		    'jpg'
		]
	    ]);
	    if ($file->save()) {
		return $file;
	    }
        }
	
	return false;
    }
}

     $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) 

    public function actionTest()
    {
	foreach (\sem\filestorage\models\File::find()->all() as $f) {
	    echo $f->getUrl(true) . "<br>";
	    echo $f->url . "<br>";
	    echo $f->name . "<br>";
	    echo $f->path . "<br>";
	    echo $f->size . "<br>";
	    echo \sem\helpers\FileHelper::formatSize($f->size) . "<br>";
	    echo "<br>";
	    echo "<br>";
	}
    }

    public function actionTest()
    {
        foreach (\sem\filestorage\models\Image::find()->all() as $f) {
            echo $f->getUrl(true) . "<br>";
            echo $f->url . "<br>";
            echo $f->name . "<br>";
            echo $f->path . "<br>";
            echo $f->size . "<br>";
            echo \sem\helpers\FileHelper::formatSize($f->size) . "<br>";
            echo $f->isImage . "<br>";

            echo "------heighten:<br>";
            $heighten = $f->heighten(300);
            echo $heighten->getUrl(true) . "<br>";
            echo $heighten->url . "<br>";
            echo $heighten->path . "<br>";
            echo \yii\helpers\Html::img($heighten->url);
            echo "<br>";

            echo "------widen:<br>";
            $widen = $f->widen(200);
            echo $widen->getUrl(true) . "<br>";
            echo $widen->url . "<br>";
            echo $widen->path . "<br>";
            echo \yii\helpers\Html::img($widen->url);
            echo "<br>";

            echo "------contain:<br>";
            $contain = $f->contain(100, 120);
            echo $contain->getUrl(true) . "<br>";
            echo $contain->url . "<br>";
            echo $contain->path . "<br>";
            echo \yii\helpers\Html::img($contain->url);
            echo "<br>";

            echo "------cover:<br>";
            $cover = $f->cover(100, 100);
            echo $cover->getUrl(true) . "<br>";
            echo $cover->url . "<br>";
            echo $cover->path . "<br>";
            echo \yii\helpers\Html::img($cover->url);
            echo "<br>";
            echo "<br>";
        }
    }