1. Go to this page and download the library: Download davidxu/yii2-upload 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/ */
davidxu / yii2-upload example snippets
use davidxu\upload\Upload;
use yii\helpers\Url;
use davidxu\base\enums\AttachmentTypeEnum;
// without ActiveForm
echo Upload::widget([
'model' => $model,
'attribute' => 'image_src',
'name' => 'image_src', // If no model and attribute pointed
'url' => Url::to('@web/upload/local'),
'clientOptions' => [
'foo' => 'bar',
],
'useFancyUI' => true, // default true
'maxFiles' => 3,
'acceptedFiles' => 'image/*',
'uploadBasePath' => 'uploads/',
// for single file,
'existFiles' => [
'id' => 1,
'name' => 'some_name.jpg',
'path' => 'some_path_for_file',
'poster' => 'some_poster', // for video or audio placeholder
'file_type' => AttachmentTypeEnum::TYPE_IMAGES,
'size' => 1111,
],
// for multiple files
// 'existFiles' => [
// [
// 'id' => 1,
// 'name' => 'some_name.jpg',
// 'path' => 'some_path_for_file',
// 'poster' => 'some_poster',
// 'file_type' => AttachmentTypeEnum::TYPE_IMAGES,
// 'size' => 1111,
// ], [
// 'id' => 2,
// 'name' => 'some_other_name.jpg',
// 'path' => 'some_path_for_other_file',
// 'poster' => 'some_poster', // for video or audio placeholder
// 'file_type' => AttachmentTypeEnum::TYPE_IMAGES,
// 'size' => 2222,
// ],
// ],
'storeInDB' => true, // return file id in DB to image url instead of file url if true, migrate model db first. default false
'metaData' => ['foo' => 'bar',],
'crop' => true, // default false, if true, the 'maxFiles' will be forced to 1
'aspectRatio' => 16 / 9, // default 1 /1
'chunkSize' => 4 * 1024 * 1024, // If `chunkSize` more than system size (`get_cfg_var('upload_max_filesize')`), system upload max filesize will be used
'secondUpload' => true, // if true, `getHashUrl` should be set
'getHashUrl' => Url::to('@web/upload/get-hash'),
]);
use davidxu\dropzone\actions\LocalAction;
use davidxu\dropzone\models\Attachment;
use yii\web\Controller;
class UploadController extends Controller
{
public function actions(): array
{
$actions = parent::actions();
return ArrayHelper::merge([
'local' => [
'class' => LocalAction::class,
'url' => Yii::getAlias('@web/uploads'), // default: ''. stored file base url,
'fileDir' => Yii::getAlias('@webroot/uploads'), // default: '@webroot/uploads'. file store in this directory,
'allowAnony' => true, // default false
'attachmentModel' => Attachment::class,
],
], $actions);
}
}
use davidxu\upload\Upload;
use yii\helpers\Url;
echo Upload::widget([
'model' => $model,
'attribute' => 'image_src',
'name' => 'image_src', // If no model and attribute pointed
'drive' => UploadTypeEnum::DRIVE_QINIU,
// ...... (refer to local config in view)
]);
use davidxu\dropzone\actions\QiniuAction;
use davidxu\dropzone\models\Attachment;
use yii\web\Controller;
use yii\web\BadRequestHttpException;
class UploadController extends Controller
{
/**
* @throws BadRequestHttpException
*/
public function beforeAction($action): bool
{
$currentAction = $action->id;
$novalidateActions = ['qiniu'];
if(in_array($currentAction, $novalidateActions)) {
// disable CSRF validation
$action->controller->enableCsrfValidation = false;
}
parent::beforeAction($action);
return true;
}
public function actions(): array
{
$actions = parent::actions();
return ArrayHelper::merge([
'qiniu' => [
'class' => QiniuAction::class,
'url' => Yii::getAlias('@web/uploads'), // default: ''. stored file base url,
'allowAnony' => true, // default false
'attachmentModel' => Attachment::class,
],
], $actions);
}
}
php composer.phar
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.