Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fcaldarelli/yii2-file-upload

File Upload for Yii2
https://github.com/fcaldarelli/yii2-file-upload

Last synced: 2 months ago
JSON representation

File Upload for Yii2

Awesome Lists containing this project

README

        

File upload for Yii2
======================

Single and Multiple file upload handler for Yii2

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist fabriziocaldarelli/yii2-file-upload "*"
```

or add

```
"fabriziocaldarelli/yii2-file-upload": "*"
```

to the require section of your `composer.json` file.

Configuration
-----

Once the extension is installed, configure it in config\main.php setting imageBaseUrl, fileUploadBasePath and fileUploadBaseUrl :

**1) Add fileUploader module to config.php**

```php
'modules' => [
'fileUploader' => [
'class' => 'sfmobile\fileUpload\Module',

// Database table name to save files metadata
'dbTableName' => 'tbl_file_upload',

'defaultStorage' => 's3prod',

'storages' => [
's3prod' => [
'class' => 'sfmobile\fileUpload\storages\S3Storage',

's3Key' => 'PROD',
's3Secret' => '1234567',
's3Endpoint' => 'https://mys3',
's3Bucket' => 'mybucket',
],
'local' => [
'class' => 'sfmobile\fileUpload\storages\FileSystemStorage',

'basePath' => '/var/www/vhosts/your_hosting/public_files',
'baseUrl' => '/public_files',
],
]
],
],
```

**2) Add the module in bootstrap section of config\main.php**

```php
'bootstrap' => ['log', 'fileUploader'],
```

**3) Apply database migration**

```
yii migrate --migrationPath=@vendor/fabriziocaldarelli/yii2-file-upload/migrations
```

Changes to Model, View and Controller
-----

I suggest to create ModelForm class that extends Model class and add an attribute to handle files (in this case, 'photo')

If you need to update other fields on database, sync() method has a last parameter $options that has this structure:

```@param $options array of [ 'filter' => [ 'andWhere' => [] ], 'saveFields' => [ 'array of fields' ] ]```

filter->andWhere data contains other field to match the record, saveFields contains set of key=>value fields to insert/update.

**Changes to Model**
```php
formTabularIndex, $this, 'photo', $this->id, 'articles', 'articles', 'photo');

return $retLoad;

}

// Delete: added files deletion
public function afterDelete()
{
parent::afterDelete();

// When delete the model, files automatically will be deleted
\sfmobile\fileUpload\FileUploadCore::deleteAll($this->id, 'articles', 'articles', 'photo');

}

// Save: after saved the model, also save the files
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);

// File upload
\sfmobile\fileUpload\FileUploadCore::sync($this, 'photo', \Yii::$app->user->identity->id, $this->id, 'articles', 'articles', 'photo');

}
```

It can be used rules validation base on file, such as:

```php
2, 'maxFiles' => 5 ],
];
}
```

**Changes to View**

Add an formSessionId hidden input field to manage (eventually!) multiple browser tabs opened in same web page (with form upload).
Then can insert your own file input widget or use that provided by the extension, derived from KartikFileInput
```php

['enctype' => 'multipart/form-data']]); ?>

= $form->field($model, 'photo')->widget(\sfmobile\fileUpload\components\kartikFileInput\KartikFileInput::className(), [
]); ?>
```

This widget is automatically configure to get validation rules from Model.

**Changes to Controller**

Create and Update actions content is the same of standard.
```php
load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}

return $this->render('create', [
'model' => $model,
]);
}

public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}

return $this->render('update', [
'model' => $model,
]);
}
```

Tabular form
--------------

In case of tabular form (multiple instances of same Model inside the same form), only the action inside the controller has to be modified. Here an example:

**Changes to Controller**
```php
andWhere(['id' => explode(',', $ids)])->all();

foreach($models as $indexModel => $model)
{
$model->formTabularIndex = $indexModel;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
}

return $this->render('update-multiple', [
'models' => $models,
]);
}
```

Models from multiple files
--------------

When you need to store multiple models from multiple files, this is the right approach:

**Changes to Model**
Inside the model, in load() you will pass an extra parameter to identify which $_FILES index to use
```php
formTabularIndex, $this, 'image', $this->id, 'banner_home', 'banner_home', 'image', $this->fileInputIndexName);

return $retLoad;

}

// Delete: added files deletion
public function afterDelete()
{
// ... as default example ...
}

// Save: after saved the model, also save the files
public function afterSave($insert, $changedAttributes)
{
// ... as default example ...
}
```

**Changes to Controller**
In Controller, you will add fileInputIndexName to pass index of $_FILES where get file data
```php
request->isPost)
{
foreach($_FILES['BannerHomeForm']['name']['image'] as $indexBanner => $n)
{
$model = new BannerHomeForm();

$model->formTabularIndex = $indexBanner;
$model->fileInputIndexName = 'BannerHomeForm[image]['.$indexBanner.']';
if ($model->load(Yii::$app->request->post()) && $model->save()) {
}
}
}
\sfmobile\fileUpload\FileUploadCore::destroySession();
```