
The validation is performed in upload() function, After validating, file saved on the server.Ĭurrently file validator check only file extensions and maxSize (which is less than 2 MB) properties, There are many other properties which can define as validation rules for uploading files. To define the validation rules on uploading image we have used FileValidator, which ensure that only png or jpg images are uploaded. In the above code, the image attribute is used to keep the instance of UploadedFile. * Function to upload image into /web/uploads directory Step 1 : Create a Model class Test.php inside models directory /models/Test.php. This class encapsulates each uploaded file as an UploadedFile object.īelow are the steps for implementing file upload functionality : There is an inbuilt class yii\web\UploadedFile in Yii2 which can be used for uploading of files. This tutorial demonstrate how we can upload an image file in an application developed on Yii2 framework. File uploading is one of the most required feature in most of applications.
