Yii Framework Forum: Chapter 4 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Chapter 4 code execution problem

#1 User is offline   mhf20 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 20-May 15

Posted 21 May 2015 - 10:45 AM

Hi, i have a problem in code execution in the chapter 4(2nd edition)
the code section: Uploading files
i use the yii 1.1.14

when i uploaded two or more files, files not uploaded.

help me, please.

Attached File(s)


0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 26 May 2015 - 06:21 AM

It could be many things. First of all, check if your php.ini is configured properly:

1. max_file_uploads should be more or equal to the number of files you're uploading.
2. upload_max_filesize should be enough to accept your file.
3. post_max_size should be more than upload_max_filesize since it will contain both upload and form data.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   mhf20 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 20-May 15

Posted 27 May 2015 - 05:00 AM

Thank you for your answer,every thing in my php.ini is correct, when the form is submitted, $files is empty and $model->attributes has one element and $_POST has 3 element, according to your code in the book, this is the result.
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 30 May 2015 - 07:22 AM

There were some changes in CFileValidator and CUploadedFile in recent versions which aren't covered by the book. I guess it could be one of these:

Quote

CFileValidator now clears attribute after validation in case attribute is marked as `safe` in `rules()` which is default. The reason is that file uploads were often handled incorrectly by using real database attribute which lead to ability to write arbitrary data into database column.

If you've followed wiki article about uploading files, you need to mark attribute as `unsafe`:

public function rules()
{
    return array(
       array('image', 'file', 'types'=>'jpg, gif, png', 'safe' => false), // <--- here
    );
}


CUploadedFile::getInstancesByName() now cannot handle a single file upload. Either change your upload field name to be something like `ImageUpload[]` or, to keep backwards compatibility in your app, handle it like the following:

$this->ImageUpload = CUploadedFile::getInstancesByName('ImageUpload');
if (empty($this->ImageUpload)) {
    $this->ImageUpload = array(CUploadedFile::getInstanceByName('ImageUpload'));
}


Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#5 User is offline   mhf20 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 20-May 15

Posted 31 May 2015 - 03:26 AM

Thank you again, but nothing changed, i don't know what to do, i tested all of the cases that I knew, for two or more files, $files is empty.
0

#6 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 31 May 2015 - 12:00 PM

In your code you have "file" not marked as "unsafe" as I've mentioned so you need to change rules() method to the following:

public function rules()
{
    return [
        ['file', 'file', 'types'=>'jpg', 'safe' => false],
    ];
}

Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#7 User is offline   mhf20 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 20-May 15

Posted 01 June 2015 - 12:13 AM

Hi Mr. makarov, every thing that you said is done and my codes is here, thank you for your helping but i did not get result.

Attached File(s)


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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