Multiple file uploads

I just wanted to comment on this post and ask this question, But as I’m new to this it doesn’t allow me,

That article was great one, but I need to upload multiple images per model, for ex: if anyone knows Drupal CCK it allows to have content field of type file, and we can upload limited or unlimited number of files for that field, and I don’t expect the same behavior, but is it available to handle this in Model and Controller and View easily instead of having static number of fields for required number of files in model class

For example if we create shopping portal and if we want to upload few pics for our product, instead of defining constant number of fields as public $image1, public $image2, etc…, is there any other way to do this using arrays and helper classes of Yii.


Well… If I’m not missing, what you want, then Yii class CMultiFileUpload should be able to solve all your problems here.

EDIT: Or if you only want to upload more than one image per one model, you can simply (following article, you cited) create more than one property in a Model, named $image1, $image2, etc. and in a form for operating on this model duplicate first file upload control as many times as many images you need to upload.

Thanks a lot, yes I wants to avoid having constant number of fields, I want user to let upload any number of images he wants, I hope CMultiFileUpload would help, I’ll check that out. This is really cool framework so I want to know every possibility of it, and it makes me to learn PHP lot easier than thought.

One more question related to CMultiFileUpload, I anyhow need to store these file locations in a database linked to my model so that next time user comes i know files uploaded by him, is it possible or is this CMultiFileUpload just to upload set of files to the server?, Sorry for troubling

Thanks for a nice words. One of the best feature of this framework is how deeply it is documented. Look for official guide, Wiki articles and many, many Yii-related blogs written by many developers from this society. If you only have time to read it, you will for sure learn quickly and find answer for most of your questions. But if there will be any problem, for which you can’t find answer, go ahead and ask. There are many people hear wanting to help. I personally never found such community ever before and elsewhere.

I’ve never used this class before. I only know that it exists and thought that can be a solution for your problem.

CMultiFileUpload is just a wrapper (or even a helper only) for jQuery Plugin jQuery Multiple File Upload. Therefore you should first read about this plugin to try to find answers for your questions. According to Yii documentation about this class, when your form is processed you can access variable $_FILES[widget-name], where you will find array containing names (and probably path) of all files that were actually uploaded. You can then use these files names to store them in DB (for example using model and ActiveRecord) in format you want.

Yes it’s true and guys are really helpful, I can see it just within a day or even less than that I get replies in this forum, I’m actually from ASP.NET MVC background, and I wanted learn PHP for sometime now, but because of it’s unstructured nature most of the time I gave up. and I tried Drupal which is so powerful but to do miracles with that you have to spend considerable time understanding its in and out, Joomla is fine but not as powerful as Drupal, Then I did search for PHP frameworks and Yii listed as top out of many, when I go through the first application I realized it is same as ASP.NET MVC (even helper classes form->labelFor etc…)

But you there is little tutorials out there compared to other CMS like Joomla Drupal or other frameworks like CakePHP, but on the other hand Yii is much easier to understand and it is just two days I started learning this. Thank you very much

There isn’t much blogs or tutorials out there as Yii isn’t that old as other frameworks you mentioned! :] It only had like third anniversary on January 1. But on the other hand I think that documentation is wonderful and with forum it helps to solve most of the problems really quick.