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
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);
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);
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.'
Nice extention + a little tip
file "UploadifyWidget.php" at line 221 change to:
private static function encode($value) { return json_encode($value); }
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:
'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()
));
queueId造成队列框设置无效
queueId造成队列框设置无效;
protected $validOptions = array(
'queueId'=>array('type'=>'string'),
中'queueId'改为'queueID'即可
bug report - Thanks
Jerry,
Fixed it in version 1.0.1
Thanks for the report
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 ","
good job!
Looking forward to more work~
Re: Nice extention + a little tip
Using json_encode for the self::encode() method is a good call, except for two things:
This is best now as:
return CJavaScript::encode( $value );
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(); }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.