Why Getinstancesbyname Won't Work In 1.1.14?


I’ve got a form, simplified:

<form enctype="multipart/form-data" action="" method="post">

  <input type="file" name="file">


In my controller:

$images = CUploadedFile::getInstancesByName('file');

When I use Yii 1.1.13 it works fine, but when updated to Yii 1.1.14 $images returns null.

I can’t believe it’s a bug, but it has to do with this Fix #159 (https://github.com/yiisoft/yii/pull/2322) right?

What am I doing wrong?

I think the form input name has to be "file[]" (or "file[0]") so it can be treated as an array.

I’m using the imperavi-redactor ext. So I can’t change the name easily I guess. I think it’s weird why they changed it in 1.1.14 this way. getInstancesByName, that suggests that you can get an Isantansce by name, not by array name.

No, the form field should be named "file[]", the getInstancesByName() method should still use "file".

Yes I misunderstood, and I changed my reply in the meantime. :rolleyes:

For the record, I’ve changed it in the redactor widget. I don’t like to do that because I have to keep track of all these changes when I update the extension. But now it works, so the file name always has to be an array I understand.

Thank you for the help!

// This works.

<form enctype="multipart/form-data" action="" method="post">

  <input type="file" name="file[]">


$images = CUploadedFile::getInstancesByName('file');

edit: one more edit. I see in the latest version of the redactor widget, the name of the file input is not hardcoded anymore, but can be set in the config. That’s great!

If you’re only uploading one file at a time, you can use “file” as the form field name and the getInstanceByName() method instead of getInstancesByName().

Oh… :mellow:

I feel kinda stupid now, totally overlooked this. :-[