Yii 1.1: euploadify

A wrapper for jquery Uploadify
6 followers

This extension adds a flash based file-upload with progress-bar and multi-file upload capability. It implements all options of Uploadify (http://www.uploadify.com)

Note: the widget defaults to a single file auto-upload widget showing a single button with 'Browse' caption.

The only required options are:

name - name of the widget

script - the controller action to call when the file(s) are received.

Documentation

Requirements

  • Yii 1.0.9 or above (have not tested with lower versions)

Installation

  • Extract the release file under protected/extensions

Usage

See http://www.uploadify.com/download/ for a full description of all options

See the following code example:

<?php 
$this->widget('application.extensions.uploadify.EuploadifyWidget', 
    array(
        'name'=>'uploadme',
        'options'=> array(
            //'uploader' => '/js/uploadify.swf',
            'script' => $this->createUrl('test/UploadedFiles'), 
            'cancelImg' => '/js/cancel.png',
            'auto' => true,
            'multi' => false,
            'folder' => '/tmp',
            'scriptData' => array('extraVar' => 1234, 'PHPSESSID' => session_id()),
            //'fileDesc' => 'Declaratiebestanden',
            //'fileExt' => '*.*',
            'buttonText' => 'Upload bestanden',
            'buttonImg' => '/images/upload.gif',
            'width' => 150,
            ),
        'callbacks' => array( 
           'onError' => 'function(evt,queueId,fileObj,errorObj){alert("Error: " + errorObj.type + "\nInfo: " + errorObj.info);}',
           'onComplete' => 'function(){alert("Complete");}',
           'onCancel' => 'function(evt,queueId,fileObj,data){alert("Cancelled");}',
        )
    )); 
?>

And put the following code in your controller:

public function actionUploadedFiles()
{
    // flash does NOT pass the session
    // thus we pass the id with a $_POST variable
    Yii::app()->session->sessionID = $_POST['PHPSESSID'];
    Yii::app()->session->init();
 
    // Do whatever you need to do with the files you just received
    $files = var_export($_FILES, true);
    $this->log('Files ' . $files);
    echo 1;
}

Change Log

November 25, 2009

  • Initial release.

November 30, 2009

  • version 1.01 - bug fix for IE

Total 11 comments

#3470 report it
wsl at 2011/04/14 09:08am
works
public function actionUploadedFiles()
    {
                if(isset($_POST['PHPSESSID']))
                {
                    Yii::app()->session->close();
                    Yii::app()->session->sessionID = $_POST['PHPSESSID'];
                    Yii::app()->session->open();
                }
                if(Yii::app()->user->isGuest) throw new CHttpException(403,'bad');
 
                //... 
 
                echo 1;
                Yii::app()->end();
    }
#3163 report it
Say_Ten at 2011/03/23 06:26am
Re: Nice extention + a little tip

Using json_encode for the self::encode() method is a good call, except for two things: - There are Yii classes that wrap this, best to stick to framework calls. - This will encode the callbacks as strings.

This is best now as:

return CJavaScript::encode( $value );
#312 report it
bitshine at 2010/07/12 09:58pm
Fw: last comma bug still in IE

sorry for wrong code fixed below:

elseif($k == 'scriptData')
{
    /*
    $sd ='';
    foreach($v as $dkey=>$dval)
    {
        $sd .= "'" . $dkey ."':'" . $dval ."',";
    }
    $es[] = "'" . $k . "':{" . $sd . "}";   
    */
    $es[] = "'" . $k . "':".json_encode($v);
#313 report it
bitshine at 2010/07/12 09:55pm
last comma bug still in IE

I use version 1.0.1, still can not use in IE. why not use json_encode, it is easy and correct.

/*
elseif($k == 'scriptData')
{
    $sd ='';
    foreach($v as $dkey=>$dval)
    {
        $sd .= "'" . $dkey ."':'" . $dval ."',";
    }
    $es[] = "'" . $k . "':{" . $sd . "}";   
*/
$es[] = "'" . $k . "':".json_encode($v);
#752 report it
YorkSEO at 2010/03/05 06:11pm
Doesn't work with CSRF Validation

Anyone had any luck getting this working with CSRF validation? I've tried passing the CSRF token in the scriptData array as well as the session id but it's failing with message 'The CSRF token could not be verified.'

#782 report it
FDisk at 2010/02/28 06:06pm
Nice extention + a little tip

file "UploadifyWidget.php" at line 221 change to:

private static function encode($value) {
    return json_encode($value);
}
#845 report it
gazbond at 2010/02/19 12:01pm
Nice extension + a little tip

You can use this extension with CUploadedFile and CFileValidator by specifying Uploadify's fileDataName property to match what CHtml::activeFileField() generates for the file inputs name attribute.

View file snippet:

$this->widget('application.extensions.uploadify.EuploadifyWidget', array(
    'model'     => $image,
    'attribute' => Image::getFileAttributeName(),
    'options'=> array(
        'fileDataName'   => 'Image[' . Image::getFileAttributeName() . ']', // wont work with out this
        'script'         => $this->createUrl('image/upload'),
        'scriptData'     => array('PHPSESSID' => session_id()),
        'auto'           => true,
        'multi'          => true,
        'fileDesc'       => 'Image Files',
        'fileExt'        => Image::getUploadifyFileTypes(),
        'queueSizeLimit' => Image::getMaxQueueSize(),
        'sizeLimit'      => Image::getMaxFileSizeBytes(),
        'buttonText'     => 'Browse Images',
        'width'          => 110,
        'height'         => 25,
        ),
    'callbacks' => array()
));
#1093 report it
.独飞. at 2009/12/11 02:34am
queueId造成队列框设置无效

queueId造成队列框设置无效; protected $validOptions = array( 'queueId'=>array('type'=>'string'), 中'queueId'改为'queueID'即可

#1139 report it
mbetel at 2009/11/30 04:23am
bug report - Thanks

Jerry,

Fixed it in version 1.0.1 Thanks for the report

#1140 report it
jerry2801 at 2009/11/30 04:04am
version 1, bug~

above line: 241 add newline use following:

$sd = substr($sd, 0, -1);

it will avert ie browser can not read array/object last comma ","

#1144 report it
jerry2801 at 2009/11/26 09:22pm
good job!

Looking forward to more work~

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: Other Open Source License
  • Developed by: mbetel
  • Category: User Interface
  • Votes: +11 / -1
  • Downloaded: 3,442 times
  • Created on: Nov 25, 2009
  • Last updated: Dec 8, 2009