Yii 1.1: muploadify

use uploadify in your application - a jQuery file upload plugin script
28 followers

widget that integrates uploadify in your application - a jQuery file upload plugin script

Requirements

Yii 1.1 or above

Usage

Usage with model:

//view
$this->widget('MUploadify',array(
  'model'=>$model,
  'attribute'=>'myAttribute',
  //'script'=>$this->createUrl('upload'),
  //'auto'=>true,
  //'someOption'=>'someValue',
));
 
//controller
function init(){
  if(isset($_POST['SESSION_ID']){
    $session=Yii::app()->getSession();
    $session->close();
    $session->sessionID = $_POST['SESSION_ID'];
    $session->open();
  }
}
function actionUpload(){
  $model=new myModel;
  if(isset($_POST['myModel'])){
    $model->myAttribute=CUploadedFile::getInstance($model,'myAttribute');
    if(!$model->save() || !$model->myAttribute->saveAs('someFile.jpg'))
      throw new CHttpException(500);
    echo 1;
    Yii::app()->end();
  }
}

using without model:

//view
$this->widget('MUploadify',array(
  'name'=>'myPicture',
  //'buttonText'=>Yii::t('application','Upload a picture'),
  //'script'=>array('myController/upload','id'=>$model->id),
  //'checkScript'=>array('myController/checkUpload','id'=>$model->id),
  //fileExt=>'*.jpg;*.png;',
  //fileDesc=>Yii::t('application','Image files'),
  //'uploadButton'=>true,
  //'uploadButtonText'=>'Upload new',
  //'uploadButtonTagname'=>'button',
  //'uploadButtonOptions'=>array('class'=>'myButton'),
  //'onAllComplete'=>'js:function(){alert("Pictures uploaded!";);}',
));
 
//controller
function init(){
  if(isset($_POST['SESSION_ID']){
    $session=Yii::app()->getSession();
    $session->close();
    $session->sessionID = $_POST['SESSION_ID'];
    $session->open();
  }
}
function actionUpload(){
  if(isset($_POST['myPicture'])){
    $myPicture=CUploadedFile::getInstanceByName('myPicture');
    if(!$myPicture->saveAs('someFile.ext'))
      throw new CHttpException(500);
    echo 1;
    Yii::app()->end();
  }
}

Notes

  • use uploadify events prepending 'js:' to its value, like this :

'onAllComplete'=>'js:function(){alert("Done!";);}',

  • you must echo 1 in the controller or it will bug the progress bar (this is a uploadify bug)

Options

string $sessionKey='SESSION_ID'

The key that will contain the session id so you can retrieve later by $_POST['SESSION_ID']

boolean $uploadButton=null

wheter to generate a button to trigger the upload. If null, it will generate the button if uploadify 'auto' option is not set or is false

array $uploadButtonOptions=array()

html options of the upload button

string $uploadButtonTagname='a'

the name of the html tag to generate the upload button

string $uploadButtonText='Upload files'

the text to be used in the upload button

mixed $script

The path to the back-end script that will process the file uploads. The path can be either an array or a string. See CHml::normalizeUrl.

See also the uploadify documentation

Defaults to the active url.

mixed $checkScript

The path to the back-end script that checks for pre-existing files on the server. The path can be either an array or a string. See CHml::normalizeUrl.

See also the uploadify documentation

Defaults to null, disabling this option.

mixed $scriptData

An object containing name/value pairs with additional information that should be sent to the back-end script when processing a file upload.

The data is json encoded and can be later retrieved by json decoding $_POST['attribute'] or $_POST['model']['attribute']

any other uploadify option

Version

  • Version 0.1 (20/08/2011): Initial release. It uses uploadify lastest stable version 2.1.4

Resources

Total 4 comments

#5827 report it
sidewinder at 2011/11/17 03:11am
Alternative configuration

Good one! I didn't like the controller init override though so I looked for an alternative. Since muplodify default session handler is same as php's default session handler all I had to do is to turn on transparent session id when appropriate. Config file:

'session'=>array(
 'class'=>'CHttpSession',
 'useTransparentSessionID'   =>($_POST['PHPSESSID']) ? true : false,
 'cookieMode'                =>($_POST['PHPSESSID']) ? 'none' : 'allow',
),
#5770 report it
Gustavo at 2011/11/10 09:20pm
Thanks

I'm glad you liked and it was useful for you.

As soon as I have time I'll update the extension with your fix.

As for the multi option, it is in the uploadify documentation, like many other options. I only posted a link here to it so I would'nt have to copy it all here and change every time it changes.

As for updates, I'll update as needed or when a new stable uploadify version is releasead.

#5766 report it
resurtm at 2011/11/10 02:18pm
+1

This extension is much better than other two. Do not forget about updates. Thanks!

#5664 report it
notsoluckycharm at 2011/10/31 12:02am
Tiny Suggestion

Love this one over the other one. A bit more documentation would be nice. For example: Multi. I figured this out. You may 1 request PER FILE to the Url specified. This is fine ( I just want to spell it out ). Also if this form is embedded in a page you have a jump problem because the "a href="#" is not returning false. This is a tiny oversight - because you can override this in the settings so it's not major... but may I recommend changing line 211 to:

$this->uploadButtonOptions['onclick']="javascript:$('#{$this->inputId}').uploadifyUpload(); return false;"; ??

Return false stops the jump to the top of the screen. Thanks!

( edited because the HTML I added messed the comment up )

Leave a comment

Please to leave your comment.

Create extension
Downloads