使用codeigniter中的一个文件字段上传多个文件

时间:2011-07-29 10:46:28

标签: php codeigniter

解决 我在网页上有一个名为additional_photos[]的文件字段,当然可以有n个此字段的实例,其中包含不同的ID但名称相同。在普通的PHP代码中,我将在foreach上执行$_FILES['additional_photos']并轻松休息。但是我如何使用CodeIgniter实现相同的目标?我试过这样做:

        $additional_photosCount = count($_FILES['additional_photos']['name']); //Is there a better way to refer $_FILES like I can refere $_POST in CI?
        for($i=0; $i< $additional_photosCount; $i++){
            $uploadConfig['file_name'] = $this->properties['userId'].'-'.$_FILES['additional_photos']['name'][$i];
            $this->CI->upload->initialize($uploadConfig);
            if(!$this->CI->upload->do_upload('additional_photos['.$i.']')){;
                echo $this->CI->upload->display_errors();
            }
        }

但是,这 a)恕我直言,方式不正确
b)给我错误“你没有选择要上传的文件。”
更新
这是我可以申请的出路:

        $additional_photosCount = count($_FILES['additional_photos']['name']);
        for($i=0; $i< $additional_photosCount; $i++){
            $uploadConfig['file_name'] = $this->properties['userId'].'-'.$_FILES['additional_photos']['name'][$i];
            $this->CI->upload->initialize($uploadConfig);
            $_FILES['additional_photos_'.$i] = array(
                'tmp_name'=> $_FILES['additional_photos']['tmp_name'][$i],
                'name'=> $_FILES['additional_photos']['name'][$i],
                'type'=> $_FILES['additional_photos']['type'][$i],
                'size'=> $_FILES['additional_photos']['size'][$i],
                'error'=> $_FILES['additional_photos']['error'][$i],
            );
            if(!$this->CI->upload->do_upload('additional_photos_'.$i)){;
                echo $this->CI->upload->display_errors();//TODO: instead of echoing push errors to a list
            }
        }

2 个答案:

答案 0 :(得分:1)

选中此项:https://github.com/nicdev/CodeIgniter-Multiple-File-Upload,应该按上述要求执行。

答案 1 :(得分:0)

HTML5 中,您可以使用此功能

如果您想让用户选择多个文件,只需在输入元素上使用multiple属性:

   <input type="file" id="input" multiple onchange="handleFiles(this.files)">

REFERENCE