Yii CMultiFileUpload select multiple files

Got an answer

To upload several files to the registration database, we tried so many ways to make several file downloads workable using the CMultiFileUpload widget. Already, I checked and followed the links below -

http://www.yiiframework.com/forum/index.php/topic/47665-multiple-file-upload/

Download multiple Yii files

BUT still no luck !!

Error: saving data, but files do not load please help here is my code:

In the shape of

<?php  $this->widget('CMultiFileUpload',
        array(
                   'model'=>$model,
                   'attribute' => 'documents',
                   'accept'=>'jpg|gif|png|doc|docx|pdf',
                   'denied'=>'Only doc,docx,pdf and txt are allowed', 
                   'max'=>4,
                   'remove'=>'[x]',
                   'duplicate'=>'Already Selected',

                )
);?>

Controller code

public function actionRegistration()
    {
        $model=new PatientRegistration;

        $this->performAjaxValidation($model);

        if(isset($_POST['PatientRegistration']))
        {
            $model->attributes=$_POST['PatientRegistration'];
            if($model->validate())
            {
                if(isset($_POST['PatientRegistration']))
                {
                    if($filez=$this->uploadMultifile($model,'documents','/Images/'))
                    {
                        $model->documents=implode(",", $filez);
                    }
                    $model->attributes=$_POST['PatientRegistration'];
                    if($model->save())
                    {
                        //  $this->render('registration',array('model'=>$model));
                        $this->redirect(array('/patientregistration/patientview','id'=>$model->register_id));
                    }
                }
            }
        }
        $this->render('registration',array('model'=>$model));
    }
    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);
        }
    }

Add to CActiveForm widget

'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),

Hence u can use this code to load multiple files in yiiframework

+4
source share
2 answers

Yii Framework

  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);
       }
     }

<?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',

       )
        );?>
+3

- , yii

, -, . Google.

0

Source: https://habr.com/ru/post/1527514/


All Articles