Yii Framework Forum: Uploading Image In Yii - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Uploading Image In Yii Rate Topic: -----

#1 User is offline   Messi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 26-March 14

  Posted 27 March 2014 - 11:32 PM

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 ideaAttached File  StudentController.php (2.3K)
Number of downloads: 1
0

#2 User is offline   Riff 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 04-June 13

Posted 28 March 2014 - 01:59 AM

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
0

#3 User is offline   Messi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 26-March 14

Posted 28 March 2014 - 03:32 AM

View PostRiff, on 28 March 2014 - 01:59 AM, said:

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

0

#4 User is offline   Messi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 26-March 14

Posted 28 March 2014 - 03:34 AM

Thank you Riff. Finally I got solution from you
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users