PHP code example of chabberwock / yii2-files

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

    

chabberwock / yii2-files example snippets


    // Basic upload
    <?= $form->field($model, 'session_id')->widget('\chabberwock\files\FileUpload') 

    $session = Yii::$app->getModule('files')->findSession($model->session_id);
    foreach ($session->listFiles() as $file) {
        $session->moveFile($file, '/public/uploads');
    }

        Yii::$app->on('files.upload', function ($event) use ($app) {
            /** @var \chabberwock\files\Session */
            $session = $event->session;
            if (isset($session->meta['target']) && $session->meta['target'] == 'embed') {
                $file = $event->file;
                $newname = uniqid() . '.' . $file->ext;
                $dirParts = [
                    '/uploads/embed',
                    date('Y'),
                    date('m'),
                    date('d'),
                    $newname
                ];
                $path = $app->basePath . implode(DIRECTORY_SEPARATOR, $dirParts);
                if (!file_exists(dirname($path))) {
                    mkdir(dirname($path), 0755, true);    
                }
                $session->moveFile($file, $path);
                chmod($path, 0755);
                $file->url = implode('/', $dirParts);
            }
        });