【问题标题】:Upload multiple files in array with yii使用 yii 上传数组中的多个文件
【发布时间】:2014-09-14 07:42:58
【问题描述】:

我在此链接 http://www.yiiframework.com/doc/guide/1.1/en/form.table 之后创建了一个带有数组字段的表单

并像这样在数组中创建了一个文件字段

echo $form->fileField($m, "[$i]myfile");

但现在我不知道我要在控制器中做什么来保存文件路径和文件名等。我可以保存其他信息,但不能保存上传文件。我试过了,但没有运气。

$imageUpload = CUploadedFile::getInstance($models[$i],'name');

【问题讨论】:

    标签: php file-upload yii


    【解决方案1】:

    试试这个方法,但首先要确保你的表单类型是

    <?php $form=$this->beginWidget('CActiveForm', array(
                'id'=>'user-form',
                'htmlOptions'=>array(
                    'enctype' => 'multipart/form-data'
                ),
            )); ?>
    
    
     //Avatar Upload on controller start
    
    
            $uploaded_file = CUploadedFile::getInstance($model,'avatar');
    
            $main_image=null;
            if($uploaded_file and $model->validate())
            {
                $main_image =time()."-".$model->username.".".$uploaded_file->getExtensionName();
    
                $model->avatar=$uploaded_file;#initialize model attribute with file name
    
                $model->avatar->saveAs(Yii::app()->basePath."/../images/profile-images/".$main_image);//this will upload selected image file
    
    
            }
     //Avatar Upload on controller end
    

    【讨论】:

      【解决方案2】:

      此方法需要浏览器支持HTML5。

      在视图文件中:

      $form=$this->beginWidget('CActiveForm', array(
          'id'=>'ca-form',
          'enableAjaxValidation'=>false,
          'htmlOptions' => array('enctype' => 'multipart/form-data'),
      ));
      ...
      echo $form->fileField($m, "myfile[]", array('multiple'=>true));
      

      在控制器中:

      $imageUploads = CUploadedFile::getInstances($models,'myfile');
      foreach ($imageUploads as $imageUpload) {
          ...// Here you can use $imageUpload->name.
      }
      

      注意 getInstances 的“s”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 2014-01-30
        • 2017-10-13
        • 1970-01-01
        • 2019-05-25
        • 2015-12-30
        相关资源
        最近更新 更多