Uploading Image In Yii

I provide all the data in the form but the error "Array ( [photo] => Array ( [0] => Photo cannot be blank. ) )"

came.Do any one have solution for this problem.

I am just learning yii and ask with other friends also and they also could not solve .reply me if you have any idea5376

StudentController.php

Which of your actions do you call?

  • actionDetail

  • actionNewStudent

  • actionUpdate

  • actionSaveStudent

I guess, you call actionSaveStudent, or not? At first, I think you should render the page at the end of the execution of your action.

Then, take a look to the following lines:




        if(isset($_POST['submit']))

        {

            $model->name=$_POST['name'];

            $model->address=$_POST['address'];

            $model->roll=$_POST['roll'];

            $rnd = rand(0,9999);  // generate random number between 0-9999

            //$model->attributes=$_POST['Student'];


            $uploadedFile=CUploadedFile::getInstance($model,'photo');

            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name

            $model->photo = Yii::app()->basePath.'/../student/'.$fileName;

            ...

        }



You give your $model the name, the address, the roll, etc. through the transmitted $_POST-data but never the file. So, if you call CUploadedFile::getInstance($model,‘photo’), the function finds an empty attribute ‘photo’ in the model.

Take a look to this link to see a possible usage of uploaded files.

I hope, I could help you.

Best regards,

Riff

Thank you Riff. Finally I got solution from you