PHP code example of ozerich / yii2-filestorage-db
1. Go to this page and download the library: Download ozerich/yii2-filestorage-db 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/ */
ozerich / yii2-filestorage-db example snippets
'components' => [
'media' => [
'class' => 'ozerich\filestorage\FileStorage',
'scenarios' => [
'avatar' => [
'storage' => [
'type' => 'file',
'saveOriginalFilename' => false,
'uploadDirPath' => __DIR__ . '/../../web/uploads/avatars',
'uploadDirUrl' => '/uploads/avatars',
],
'validator' => [
'maxSize' => 2 * 1024 * 1024, // 2 MB
'checkExtensionByMimeType' => true,
'extensions' => ['jpg', 'jpeg', 'bmp', 'gif', 'png']
],
'thumbnails' => [
[
'width' => 500
],
[
'height' => 500
],
[
'alias' => 'preview',
'width' => 250,
'сrop' => true,
'2x' => true,
'force' => true,
'webp' => true
],
[
'width' => 200,
'height' => 200,
'exact' => true
],
],
'quality' => 75
],
'document' => [
'storage' => [
'type' => 'file',
'uploadDirPath' => __DIR__ . '/../../web/uploads/documents',
'uploadDirUrl' => '/uploads/documents',
],
'validator' => [
'maxSize' => 20 * 1024 * 1024, // 20 MB
'checkExtensionByMimeType' => true,
'extensions' => ['pdf', 'doc'],
],
]
]
]
]
'config' => [
'bootstrap' => [..., 'media'],
]
'config' => [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'ozerich\filestorage\migrations',
],
],
],
]
/* app\controllers\UploadController.php */
public function actionImage()
{
Yii::$app->response->format = 'json';
$file = UploadedFile::getInstanceByName('file');
$model = Yii::$app->media->createFileByUploadedFile($file, 'avatar');
return [
'image' => $model->toJSON()
];
}
/* app\models\User.php */
public function setUserAvatarFromUrl($image_url)
{
$image = $media->createFileFromUrl($image_url, 'avatar');
$this->avatar_image_id = $image->id;
}
/* app\controllers\UploadController.php */
public function actionImage()
{
Yii::$app->response->format = 'json';
$base64string = Yii::$app->request->post('data');
$filename = Yii::$app->request->post('filename');
$model = Yii::$app->media->createFileFromBase64($base64string, $filename, 'avatar');
return [
'image' => $model->toJSON()
];
}
php composer.phar
php yii migrate/up