1. Go to this page and download the library: Download floor12/yii2-module-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/ */
public function behaviors()
{
return [
'files' => [
'class' => 'floor12\files\components\FileBehaviour',
'attributes' => [
'avatar',
'documents'
],
],
...
public function attributeLabels()
{
return [
...
'avatar' => 'Аватар',
'documents' => 'Документы',
...
];
}
public function rules()
{
return [
// Avatar is llow to uploade 1 file with this extensions: jpg, png, jpeg, gif
['avatar', 'file', 'extensions' => ['jpg', 'png', 'jpeg', 'gif'], 'maxFiles' => 1],
// Documens allows to upload a few files with this extensions: docx, xlsx
['documents', 'file', 'extensions' => ['docx', 'xlsx'], 'maxFiles' => 10],
...
// The href field contains web path to file source
echo Html::img($model->avatar->href)
// __toString() method of File object will return href as well
echo Html::img($model->avatar)
// User avatar thumbnail with 200px width
echo Html::img($model->avatar->getPreviewWebPath(200));
// User avatar thumbnail with 200px width and WEBP format
echo Html::img($model->avatar->getPreviewWebPath(200, 0, true));
foreach ($model->docs as $doc}
Html::a($doc->title, $doc->href);