Yii CMultiFileUpload选择多个文件

时间:2014-02-18 12:52:42

标签: php yii

得到了答案

将多个文件上传到数据库进行注册   尝试了很多方法,使用CMultiFileUpload小部件使多个文件上传可行。我已经检查并按照以下链接 -

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

Yii多文件上传

但仍然没有运气!!

错误:存储数据但文件未上传 请帮忙 这是我的代码:

格式

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

                )
);?>

控制器代码

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

添加CActiveForm小部件

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

因此,您可以使用此代码在yiiframework中上传多个文件

2 个答案:

答案 0 :(得分:3)

以下是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',

       )
        );?>

答案 1 :(得分:0)

这一切都是为了多次上传 - 这很不错,但对于多个选择you can try this yii extension

我希望这个链接可以帮助某些人,因为我正在努力使用多个选择文件进行多次上传。花了很多时间在Google搜索中。干杯