Uploading files

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. ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.