使用yii上传数组中的多个文件

时间:2014-09-14 07:42:58

标签: php file-upload yii

我在此链接http://www.yiiframework.com/doc/guide/1.1/en/form.table

之后创建了一个包含数组字段的表单

并在数组中创建了一个像

这样的文件字段
echo $form->fileField($m, "[$i]myfile");

但是现在我不知道我将在控制器中做什么来保存文件路径和文件名等。我能够保存其他信息但不能保存上传文件。我试过这个但没有运气。

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

2 个答案:

答案 0 :(得分:0)

尝试这种方式,但首先要确保您的表单类型为

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

答案 1 :(得分:0)

此方法需要浏览器支持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'

相关问题