Yii CMultiFileUpload select multiple files

后端 未结 2 731
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 22:29

Got The Answer

To upload multiple file in to the database for registration have tried so many ways to make multiple file upload workable using CMultiFileUpload wi

相关标签:
2条回答
  • 2021-01-13 23:05

    Here is the simplest Code for Multiple File Upload in Yii Framework

    code

    In Controller

      public function actionCreate()
    
        {
            $model = new Upload;
           echo Yii::app()->basePath.'/Images/';
           if(isset($_POST['Upload']))
            {
        if($filez=$this->uploadMultifile($model,'Document','/Images/'))
       {
    
       $model->Document=implode(",", $filez);
       }
       $model->attributes=$_POST['Upload'];
        if ($model->save())
                {
                   $this->redirect(array('view', 'id' => $model->idUpload));
            }
            }
            $this->render('create', array(
                'model' => $model,
             ));
    
        }
    //Function for uploading and saving Multiple files
        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();
            $fileName = "{$sfile[$i]}";
              $formatName=time().$i.'_'.$fileName;
             $file->saveAs(Yii::app()->basePath.$path.$formatName);
             $ffile[$i]=$formatName;
             }
            return ($ffile);
           }
         }
    

    In Form

    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'upload-form',
        // Please note: When you enable ajax validation, make sure the corresponding
        // controller action is handling ajax validation correctly.
        // There is a call to performAjaxValidation() commented in generated controller code.
        // See class documentation of CActiveForm for details on this.
        'enableAjaxValidation'=>false,
        'htmlOptions' => array(
            'enctype' => 'multipart/form-data',
        ),
    )); ?>
    
    
    <?php  $this->widget('CMultiFileUpload',
      array(
           'model'=>$model,
           'attribute' => 'Document',
           'accept'=>'jpg|gif|png|doc|docx|pdf',
           'denied'=>'Only doc,docx,pdf and txt are allowed', 
           'max'=>4,
           'remove'=>'[x]',
           'duplicate'=>'Already Selected',
    
           )
            );?>
    
    0 讨论(0)
  • 2021-01-13 23:12

    That was all up is for multiple upload - that's nice, but as for multiple select you can try this yii extension

    I hope that this link will help someone, because I was struggling with multiple select files for multiple upload then. Spent lot of time in Google search. Cheers

    0 讨论(0)
提交回复
热议问题