Coco! Multi file Uploader Widget
by: Christian Salazar. christiansalazarh@gmail.com @yiienespanol, oct. 2012.

Ver la Documentación en Español
http://opensource.org/licenses/bsd-license.php
**Requirement: Yii 1.1.11 ** if you are using a lower yii version then you will receive an error about "CJavaScriptExpression", please refer to issues about how to solve this issue.
'Coco' is a widget for yii framework designed to handle File Uploads (Single and Multiple). Is designed using Ajax-jQuery and a well formed Architecture based on MVC (and UML). Using 'coco' is very simple, at first place you setup a fixed action in any controller, this action serves for all every coco-widgets in your application. At second place you insert the widget in your form, all uploaded files will be stored in the path provided by 'uploadDir' widget attribute. Very simple and usefull. Please enjoy it.
Coco takes its functionality from a very nice PHP library located at valums repository in github, all licences are explicit in coco related to Valums work. Go to the official valums google group.
cd /home/blabla/myapp/protected
mkdir extensions
cd extensions
git clone https://christiansalazarh@bitbucket.org/christiansalazarh/coco.git
If you dont use GIT please copy the entire 'coco' folder into your extensions folder
Please download the provided zip, and uncompress it into /protected/extensions/coco.
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.coco.*', // <------
),
Connect the widget with your current application using a fixed Action in siteController (or a distinct controller if you prefer).
By default, using:
myapp/protected/controllers/SiteController.php
IMPORTANT: This action is required only one time for all above project !!
public function actions() { return array( 'captcha'=>array( 'class'=>'CCaptchaAction', 'backColor'=>0xFFFFFF, ), 'page'=>array( 'class'=>'CViewAction', ), 'coco'=>array( 'class'=>'CocoAction', ), ); }
$this->widget('ext.coco.CocoWidget' ,array( 'id'=>'cocowidget1', 'onCompleted'=>'function(id,filename,jsoninfo){ }', 'onCancelled'=>'function(id,filename){ alert("cancelled"); }', 'onMessage'=>'function(m){ alert(m); }', 'allowedExtensions'=>array('jpeg','jpg','gif','png'), // server-side mime-type validated 'sizeLimit'=>2000000, // limit in server-side and in client-side 'uploadDir' => 'assets/', // coco will @mkdir it // this arguments are used to send a notification // on a specific class when a new file is uploaded, 'receptorClassName'=>'application.models.MyModel', 'methodName'=>'onFileUploaded', 'userdata'=>$model->primaryKey, // controls how many files must be uploaded 'maxUploads'=>3, // defaults to -1 (unlimited) 'maxUploadsReachMessage'=>'No more files allowed', // if empty, no message is shown // controls how many files the can select (not upload, for uploads see also: maxUploads) 'multipleFileSelection'=>true, // true or false, defaults: true ));
Coco will invoke an specific method name (methodName) in an specific class provided in widget config (receptorClassName). When a new file arrives from upload the Coco will invoke this method passing to you the 'userdata' argument and the full file path.
By example, suppose you have /protected/models/MyModel.php and you need the uploaded file arrives in this class, so the widget config will be:
'receptorClassName'=>'application.models.MyModel', 'methodName'=>'myFileReceptor', 'userdata'=>$model->primaryKey,
Your class and method in MyModel would be:
class MyModel { public function myFileReceptor($fullFileName,$userdata) { // userdata is the same passed via widget config. } }
Check your php.ini for post_max_uploads variable, and please be consistent with 'sizeLimit'=>2000000.
Check log for errors. Coco will write an error log when something goes wrong.
'buttonText'=>'Find & Upload', 'dropFilesText'=>'Drop Files Here !', 'htmlOptions'=>array('style'=>'width: 300px;'), 'defaultControllerName'=>'site', 'defaultActionName'=>'coco',
Total 20 comments
How to get only the file name you are uploading.
@buyell, this is awesome news ... I do respect your work and I can imagine what it brings to improve/edit the valums code! I look forward to seeing it, and I think we all do :)
Keep doing the awesome job ! Cheers
@Skylight, yes you are in right. due to this extension is a big wrapper for its original valums file uploader, i must get involved in the internals of this author in order to provide this feature..possible consecuence: bug. but, it is in mind to make this enhancement.
Hi,
It's a very helpful and nice extension and I may have a suggestion to further improve it. It would be nice if you can add a delete button or an "x" link next to the uploaded file, in case the user changes his mind on the uploaded files.
PS: It would also be nice to add different icons before the file name preview. In other words, here is a screenshot showing the new features Screenshot
I hope you consider my suggestion :) But anyway, +1 from me, it helped me a lot! Cheers
this last commit has maxUploads support.
https://bitbucket.org/christiansalazarh/coco/get/0e06b51843c0.zip
download or pull the new version, it has solved the issue about UrlManager (nice urls). Sorry for late.
i dont understand your point, please create an issue in coco repo. thank u.
thanks for report it. it is solved and published.
this is because coco doesnt work with path based urls. in a short comming release i resolve this issue.
http://site.com/index.php/coco/coco&action=upload&nocache=135597771...
coco is based on a well and hard tested product: "the valums ajax file uploader", coco is a wrapper only, if coco is so bad and insecure then valums file uploader is the same too, and i think this is not the case.
Coco provides server side validation to check image type (based or not in mime) this is not the business for coco widget, is yours, coco only provide the uploaded file to your server.
could you please write all this things in the proper issue location on repo ? thanks for it.
Your widget don't use parameters(size, type,...) on client side and don't check it on client side! just send parameters to server and check it on server, this scenario is wrong and not fastest(because user must first upload and then check file type and valid extensions) and not secure (because any user can change valid file size or valid file extensions on client and upload any files!) !
in the html output widget see:
see double single quotation in end line!
I find a very important security bug in runAction function. You use the parameters sent from client and set its for current object on server and save it, so If the user change this parameter for example change valid extensions by firebug and submit form, by easily can upload any file with any extensions on server!!!
I do not understand why after uploading, the path files are stored on the server by Ajax should be sent to the client?
path "upload/" fullpath "upload/filename.jpg"
I thinks isn't a security work.
Hi,
After upload completed, return error 404, the return url is:
http://site.com/index.php/coco/coco&action=upload&nocache=135597771...
I thinks must have a "?" after controller/action , I resolved this with put "?" after defaultActionName I suggest to you, use one parameter for route and create url with createUrl yii function for create url from route and append data parameters.
Thanks
You can manage the uploaded images using this widget: Imagegallery1 Ajax Image Manager
May be user upload missing file So then I need function delete file.
Thank a lot for useful extension.>
I mean it can delete or remove file from the list before upload.
bug. Coco always allow uploads using any file extension no matter if is allowed or not in 'allowedExtensions'.
in file CocoWidget.php line number 179, please change:
$uploader = new ValumsFileUploader(array(), $this->sizeLimit);
for this:
$uploader = new ValumsFileUploader($this->allowedExtensions, $this->sizeLimit);
review the issue at: review code changes to solve issue #1
I tried it, it's really a usefull extension.
Just a note. if you set:
it should be unnecessary write:
to use:
should be enough.
thanks for your contribution, Cheers
Leave a comment
Please login to leave your comment.