The common task when data is sent from view to controller is uploading files. Also, in this case, Yii2 provides a convenient helper to handle this task: yii\web\UploadedFile
. This class has two important methods: getInstance()
(in plural form getInstances()
) and saveAs()
.
The first method, getInstance()
, allows us to get the file from the form's input field, while the second method, saveAs()
, as its name implies, allows us to save file input field content to the server filesystem.
Before we start with the example, it is important to create a folder that will contain the uploaded files. The best place to create this folder is at the root directory of the application. So create a folder named uploadedfiles
under the basic/
folder.
Next, to centralize configuration, define an alias for this new folder, so that we can change this path from app configuration. Enter in basic/config/web.php
and append the aliases
property, if it does not exist...