PHP code example of studio255 / yii2-fileupload

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

    

studio255 / yii2-fileupload example snippets


use studio255\fileupload\FileUpload;
use yii\helpers\Url;

echo FileUpload::widget([
    'id'            => 'my-upload',
    'url'           => Url::to(['/upload/handle']),
    'acceptedFiles' => 'image/*',   // e.g. 'image/*,application/pdf'
    'maxFiles'      => 5,
    'multiple'      => true,
]);

use studio255\fileupload\UploadHandler;

public function actionHandle()
{
    return UploadHandler::processUpload([
        'targetPath' => \Yii::getAlias('@webroot/uploads'),
        // optional model binding:
        // 'modelClass' => app\models\MyModel::class,
    ]);
}

UploadHandler::processUpload([
    // Paths
    'targetPath' => \Yii::getAlias('@webroot/uploads'),  // default
    'tmpPath'    => \Yii::getAlias('@runtime/fileupload'), // chunk temp dir

    // Model binding (optional)
    'modelClass' => app\models\MyModel::class,
    // model_id and model_attribute are read from GET params by the widget

    // Image variants (optional)
    // 'createVariants' => true,  // generates a_ and w_ prefix files
    // 'a_' => [null, 200],       // thumbnail: height 200 px, keep aspect ratio
    // 'w_' => [800, null],       // preview: width 800 px, keep aspect ratio
]);

echo FileUpload::widget([
    'options' => [
        'instances' => [
            [
                'id'            => 'pond-cover',
                'url'           => Url::to(['/upload/handle']),
                'multiple'      => false,
                'acceptedFiles' => 'image/*',
            ],
            [
                'id'       => 'pond-gallery',
                'url'      => Url::to(['/upload/handle']),
                'multiple' => true,
                'maxFiles' => 10,
            ],
        ],
    ],
]);