PHP code example of razonyang / yii2-uploader

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

    

razonyang / yii2-uploader example snippets


return [
    'components' => [
        'filesystem' => [
            'class' => \creocoder\flysystem\LocalFilesystem::class,
            'path' => '@webroot/resources',
        ],
        'uploader' => [
            'class' => \RazonYang\Yii2\Uploader\Uploader::class,
            'host' => 'http://localhost/resources', // the hostname relative to your uploaded files
            'filesystem' => 'filesystem',
        ],
    ],
];

class UploadForm extends \yii\base\Model
{
    use \RazonYang\Yii2\Uploader\UploadModelTrait;

    public function handle()
    {
        if (!$this->validate()) {
            // handles error
            throw new \Exception('invalid file');
        }

        $url = $this->upload();
        return [
            'url' => $url,
            // ... other information
        ];
    }
}

class UploadController extends \yii\web\Controller
{
    public function actionUpload()
    {
        $form = new UploadForm([
            'file' => \yii\web\UploadedFile::getInstanceByName('file')
        ]);
        return $form->handle();
    }
}