O'Reilly logo

Yii2 By Example by Fabrizio Caldarelli

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required