yii2-dynamicform demo2 File Upload

Can somebody please explain How images are saved in wbraganca/yii2-dynamicform Demo 2 (File Upload)?

$modelOptionValue->img = \yii\web\UploadedFile::getInstance($modelOptionValue, "[{$index}]img");

Kindly explain this "img" property. and How is it saving data in table.

img after the assignment it will be an instance of UploadedFile or null(no file uploaded)

then you should validate the ‘file’ rule first :


         // validation passed 

   $someDistPath = 'some Dir for saving your uploaded file'.'/xxx.jpg' ;// you should building a destination path for saving the uploaded file . file ext can be UploadedFile::getExtension()


   //  then save the file


   $saveResult =  $modelOptionValue->img->saveAs($someDistPath);

   if($saveResult == true){

          // you have saved the file successfully !

         //  then change the path

           $modelOptionValue->img =  $someDistPath ; // note the "Type" changed from UploadedFile to String 


       // .....




after you save the file and get the distPath , you can do anything you want (it is just a string – but you should transform it to a relative URI normally and when it is displaying to browser you should transform it to a URL )