Commit 63fb997c by Alexander Makarov

Fixed formatting, added note about work in progress

parent 9d7c14e9
# Upload File with Yii2 Uploading Files
===============
### First you need to create a model that will handle the form of download the file. > Note: This section is under development.
First you need to create a model that will handle the form of download the file
-------------------------------------------------------------------------------
```php ```php
namespace app\models; namespace app\models;
...@@ -29,9 +33,13 @@ class UploadForm extends Model ...@@ -29,9 +33,13 @@ class UploadForm extends Model
} }
} }
``` ```
In this code, we created a model ```UploadForm``` with an attribute ```$file``` that will be is ```<input type="file">``` in upload form and pointed out to him validation rule ```file```. This rule is [[yii\validators\FileValidator|FileValidator]]
### Secondly create a view for our model. In this code, we created a model `UploadForm` with an attribute `$file` that will be is `<input type="file">` in upload
form and pointed out to him validation rule `file`. This rule is [[yii\validators\FileValidator|FileValidator]].
Secondly create a view for our model
------------------------------------
```php ```php
<?php <?php
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
...@@ -45,9 +53,12 @@ $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ...@@ -45,9 +53,12 @@ $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
``` ```
It is different attribute ```'enctype' => 'multipart/form-data'``` from the standard form. This value is required when you are using forms that have a file upload control. ```fileInput()``` represents a form input field. It is different attribute `'enctype' => 'multipart/form-data'` from the standard form. This value is required when you
are using forms that have a file upload control. `fileInput()` represents a form input field.
Thirdly, that create the controller that will connect our form and model
------------------------------------------------------------------------
### Thirdly, that create the controller that will connect our form and model.
```php ```php
namespace app\controllers; namespace app\controllers;
...@@ -74,26 +85,34 @@ class SiteController extends Controller ...@@ -74,26 +85,34 @@ class SiteController extends Controller
} }
} }
``` ```
The difference here from the standard crud action, so use ```UploadedFile::getInstance(...)``` instead ```model->load(...)```. [[\yii\web\UploadedFile|UploadedFile]] does not run the model validation, it only provides information about the uploaded file. Therefore, you need to run validation manually ```$model->validate()```. This triggers the [[yii\validators\FileValidator|FileValidator]] that expects a file
The difference here from the standard crud action, so use `UploadedFile::getInstance(...)` instead `model->load(...)`.
[[\yii\web\UploadedFile|UploadedFile]] does not run the model validation, it only provides information about the uploaded
file. Therefore, you need to run validation manually `$model->validate()`. This triggers the
[[yii\validators\FileValidator|FileValidator]] that expects a file
```php ```php
$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE //in code framework $file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE //in code framework
``` ```
If validation done without errors, then save the file If validation done without errors, then save the file
```php ```php
$model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension); $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
``` ```
If you use "basic" application then forlder ```uploads``` should be create inside ```web``` folder.
Everything is ready, now run the page and download the file. Check the folder ```basic/web/uploads``` to make sure that you have downloaded. If you use "basic" application then forlder `uploads` should be create inside `web` folder.
## Additional information. Everything is ready, now run the page and download the file. Check the folder `basic/web/uploads` to make sure that
you have downloaded.
*** Additional information
----------------------
### Required rule ### Required rule
If you need to check the mandatory download the file, then use ```skipOnEmpty```. If you need to check the mandatory download the file, then use `skipOnEmpty`.
```php ```php
public function rules() public function rules()
{ {
...@@ -103,17 +122,15 @@ public function rules() ...@@ -103,17 +122,15 @@ public function rules()
} }
``` ```
***
### Path upload folder ### Path upload folder
Folder to download the file can be installed using ```Yii::getAlias('@app/uploads')```. This base path of currently running application and folder ```uploads`` Folder to download the file can be installed using `Yii::getAlias('@app/uploads')`. This base path of currently running
application and folder `uploads`.
***
### MIME type ### MIME type
FileValidator have property ```$types``` FileValidator has property `$types`
```php ```php
public function rules() public function rules()
{ {
...@@ -122,16 +139,20 @@ public function rules() ...@@ -122,16 +139,20 @@ public function rules()
]; ];
} }
``` ```
it pulls it pulls
```php ```php
in_array(strtolower(pathinfo($file->name, PATHINFO_EXTENSION)), $this->types, true)) in_array(strtolower(pathinfo($file->name, PATHINFO_EXTENSION)), $this->types, true))
``` ```
As you can see, the name of the expansion may be one and the file type - other, actually. As you can see, the name of the expansion may be one and the file type - other, actually.
``UploadedFile::getInstance()->type``` also do not take this value for granted. `UploadedFile::getInstance()->type` also do not take this value for granted. Instead, use
Instead, use [[\yii\helpers\BaseFileHelper|FileHelper]] and his [[FileHelper::getMimeType()]] to determine the exact MIME type. [[\yii\helpers\BaseFileHelper|FileHelper]] and his [[FileHelper::getMimeType()]] to determine the exact MIME type.
If allowed to **load only the images**, using [[\yii\validators\ImageValidator|ImageValidator]] instead [[yii\validators\FileValidator|FileValidator]]. If allowed to **load only the images**, using [[\yii\validators\ImageValidator|ImageValidator]] instead
[[yii\validators\FileValidator|FileValidator]].
```php ```php
public function rules() public function rules()
...@@ -141,15 +162,13 @@ public function rules() ...@@ -141,15 +162,13 @@ public function rules()
]; ];
} }
``` ```
```ImageValidator``` use use ```yii\helpers\FileHelper;``` for check mime types.
[List Mime types](http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types)
*** `ImageValidator` use use `yii\helpers\FileHelper;` for check mime types.
[List Mime types](http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types)
### Multiple files uploader ### Multiple files uploader
If you need download multiple files, you will need to alter slightly the controller and view. If you need download multiple files, you will need to alter slightly the controller and view. At first view:
At first view:
```php ```php
<?php <?php
...@@ -172,18 +191,22 @@ if ($model->hasErrors()) { //it is necessary to see all the errors for all the f ...@@ -172,18 +191,22 @@ if ($model->hasErrors()) { //it is necessary to see all the errors for all the f
``` ```
In fact the only difference is in the one row. In fact the only difference is in the one row.
```php ```php
<?= $form->field($model, 'file[]')->fileInput(['multiple' => '']) ?> <?= $form->field($model, 'file[]')->fileInput(['multiple' => '']) ?>
``` ```
instead instead
```php ```php
<?= $form->field($model, 'file')->fileInput() ?> <?= $form->field($model, 'file')->fileInput() ?>
``` ```
* ```['multiple' => '']``` - HTML <input> multiple Attribute * `['multiple' => '']` - HTML <input> multiple Attribute
* ```file[]``` vs ```file`` - need, otherwise UploadedFile sees only one file * `file[]` vs `file` - need, otherwise UploadedFile sees only one file
We now turn to the controller We now turn to the controller
```php ```php
namespace app\controllers; namespace app\controllers;
...@@ -230,6 +253,9 @@ class SiteController extends Controller ...@@ -230,6 +253,9 @@ class SiteController extends Controller
} }
} }
``` ```
Here the differences in: Here the differences in:
* ``` UploadedFile::getInstances($model, 'file');``` instead ``` UploadedFile::getInstance($model, 'file');```. First returns **all** uploaded files for the given model attribute, second - one.
* `UploadedFile::getInstances($model, 'file');` instead `UploadedFile::getInstance($model, 'file');`. First returns
**all** uploaded files for the given model attribute, second - one.
* All other differences follow from the first. * All other differences follow from the first.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment