Multi File Uploader

I followed this tutorial posted: at Here


How to Upload Multiple Files in Yii Forms

Yii have very simple way to Upload Multiple Files with Jquery Interface. It include all three level integration Model , Controller and View.


Step 1 : Model 

Add Rule for File Upload Validation . Here we only allow user to upload documents with the File Extention .doc, .docx, .pdf and .txt. The same you customize for Photo or Image Upload.


array('attachments', 'file', 

'types'=>'doc,docx,pdf,txt',

'maxSize'=>1024 * 1024 * 1, // 1MB

'tooLarge'=>'The file was larger than 1MB. Please upload a smaller file.',

'allowEmpty'=>1,

)Step 2 : Controller

Controller handle main File Uploading process. we use CUploadedFile to upload multiple instances of file.




// I PUT THIS IN MY ACTION CREATE

 if($filez=$this->uploadMultifile($model,'attachments','/uploads/doc/'))

   {

   $model->attachments=implode(",", $filez);

   }


//Place the above Php Code in your Action Function and then add the below function any where  in your controller.





public function uploadMultifile ($model,$attr,$path)

{

/*

 * path when uploads folder is on site root.

 * $path='/uploads/doc/'

 */

if($sfile=CUploadedFile::getInstances($model, $attr)){

  foreach ($sfile as $i=>$file){  

     $formatName=time().$i.'.'.$file->getExtensionName();

     $file->saveAs(Yii::app()->basePath .DIRECTORY_SEPARATOR.'..'. $path.$formatName);

     $ffile[$i]=$formatName;

     }

    return ($ffile);

   }

 }Step 3 : View

In View we use CMultiFileUpload Widget to Upload Files.


<div class="row">

<?php echo $form->labelEx($model,'attachments'); ?>

<?php  $this->widget('CMultiFileUpload',

  array(

       'model'=>$model,

       'attribute' => 'attachments',

       'accept'=> 'doc|docx|pdf|txt',

       'denied'=>'Only doc,docx,pdf and txt are allowed', 

       'max'=>4,

       'remove'=>'[x]',

       'duplicate'=>'Already Selected',

                          

       )

        );?>

<?php echo $form->error($model,'attachments'); ?>

<div class="hint">You can upload up to four attachments. </div>

</div>

I GET THE FOLLOWING ERROR:


mb_strlen() expects parameter 1 to be string, array given


/home/content/t/h/e/them2765/html/cmms/framework/validators/CStringValidator.php(72)


60      * Validates the attribute of the object.

61      * If there is any error, the error message is added to the object.

62      * @param CModel $object the object being validated

63      * @param string $attribute the attribute being validated

64      */

65     protected function validateAttribute($object,$attribute)

66     {

67         $value=$object->$attribute;

68         if($this->allowEmpty && $this->isEmpty($value))

69             return;

70 

71         if(function_exists('mb_strlen') && $this->encoding!==false)

72             $length=mb_strlen($value, $this->encoding ? $this->encoding : Yii::app()->charset);

73         else

74             $length=strlen($value);

75 

76         if($this->min!==null && $length<$this->min)

77         {

78             $message=$this->tooShort!==null?$this->tooShort:Yii::t('yii','{attribute} is too short (minimum is {min} characters).');

79             $this->addError($object,$attribute,$message,array('{min}'=>$this->min));

80         }

81         if($this->max!==null && $length>$this->max)

82         {

83             $message=$this->tooLong!==null?$this->tooLong:Yii::t('yii','{attribute} is too long (maximum is {max} characters).');

84             $this->addError($object,$attribute,$message,array('{max}'=>$this->max));

Stack Trace

#0	

+  /home/content/t/h/e/them2765/html/cmms/framework/validators/CStringValidator.php(72): mb_strlen(array("101 Session I.docx", "yii-1.0-cheatsheet.pdf"), "UTF-8")

#1	

+  /home/content/t/h/e/them2765/html/cmms/framework/validators/CValidator.php(192): CStringValidator->validateAttribute(Assets, "filename")

#2	

+  /home/content/t/h/e/them2765/html/cmms/framework/base/CModel.php(152): CValidator->validate(Assets, null)

#3	

+  /home/content/t/h/e/them2765/html/cmms/framework/db/ar/CActiveRecord.php(780): CModel->validate(null)

#4	

–  /home/content/t/h/e/them2765/html/cmms/protected/controllers/AssetsController.php(77): CActiveRecord->save()

72         // $this->performAjaxValidation($model);

73 

74         if(isset($_POST['Assets']))

75         {

76             $model->attributes=$_POST['Assets'];

77             if($model->save())

78              

79                        

80                 $this->redirect(array('view','id'=>$model->id));

81         }

82 

#5	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/actions/CInlineAction.php(50): AssetsController->actionCreate()

#6	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/CController.php(300): CInlineAction->runWithParams(array("r" => "assets/create"))

#7	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/filters/CFilterChain.php(134): CController->runAction(CInlineAction)

#8	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/filters/CFilter.php(41): CFilterChain->run()

#9	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/CController.php(1144): CFilter->filter(CFilterChain)

#10	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#11	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#12	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/CController.php(283): CFilterChain->run()

#13	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/CController.php(257): CController->runActionWithFilters(CInlineAction, array("accessControl"))

#14	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/CWebApplication.php(277): CController->run("create")

#15	

+  /home/content/t/h/e/them2765/html/cmms/framework/web/CWebApplication.php(136): CWebApplication->runController("assets/create")

#16	

+  /home/content/t/h/e/them2765/html/cmms/framework/base/CApplication.php(158): CWebApplication->processRequest()

#17	

–  /home/content/t/h/e/them2765/html/cmms/index.php(13): CApplication->run()

08 defined('YII_DEBUG') or define('YII_DEBUG',true);

09 // specify how many levels of call stack should be shown in each log message

10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

11 

12 require_once($yii);

13 Yii::createWebApplication($config)->run();

14 ?>

this is a private blog. Cannot access without an invitation.