PHP code example of kak / storage

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

    

kak / storage example snippets


/** @docs https://console.aws.amazon.com/iam/home Generation access key and secret */
$amazon_config = [
    'key' => '', 
    'secret' => '',
    'bucket' => 'my',
    'level'  => 2,
    'type'   => 'amazon',
    'region' => 'us-east-1',
]

//...
'storage' => [
    'storages' => [
         // use amazon config
        'photo'  => $amazon_config,
        'custom_name' => [],
        // local server save files
        'tmp'  => [       
            'level' => 0,
        ],
    ],
],


    public function actions()
    {
         return [
             'upload' => [
                 'class' => UploadAction::className(),
                 'form_name' => 'kak\storage\models\UploadForm',
                 'storage'  => 'tmp',   // save image default tmp storage
                 'resize_image' => [    // list formats
                     'preview'   => [1024,1024, UploadAction::IMAGE_RESIZE, 'options' => [] ],
                     'thumbnail' => [120,120, UploadAction::IMAGE_THUMB],
                     '350'       => [350,280, UploadAction::IMAGE_RESIZE],
                 ]
             ],
         ];
     }


    /**
    * @param $id int edit post
    * @return string
    */         
    public function actionUpdate($id)
    {
        $model = $this->findPostById($id);
        $uploadForm = new \kak\storage\models\UploadForm(['meta_name' => 'image_base']);
        $uploadForm->meta = $postModel->images_json;

        if($this->savePostForm($model, $uploadForm)) {
            return $this->redirect(['/dashboard/post/update','id' => $postModel->id]);
        }
        return $this->render('form',compact(
            'model', 'uploadForm'
        ));
    }

    /**
     * @param $model Post
     * @param $uploadForm \kak\storage\models\UploadForm
     * @return bool
     */
    protected function savePostForm(&$model,&$uploadForm)
    {
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $result = $uploadForm->saveToStorage('tmp','images',[]);
            $model->images_json = Json::encode($result);
            
            return $model->save();
        }
        
        return false;
    }

<div>
    <?=\kak\storage\Upload::widget([
        'model' => $uploadFormModel,
        'url' => ['/dashboard/default/upload', 'resize_type' => 'thumbnail,350']    
    ]); 

StorageAsset::register($view);

php composer.phar