简体   繁体   English

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

[英]uploading multiple files using one file field in codeigniter

SOLVED I have a file field named additional_photos[] on a web page, there can be n number of instances of this field of course with different ID but same name. 解决了我在网页上有一个名为additional_photos[]的文件字段,当然可以有n个此字段的实例,它们的ID不同但名称相同。 In a normal PHP code I will do a foreach on $_FILES['additional_photos'] and will do rest easily. 在普通的PHP代码中,我将在$_FILES['additional_photos']上进行foreach并将轻松完成。 But how do I achieve the same with CodeIgniter? 但是如何使用CodeIgniter达到相同的目的? I tried doing this: 我尝试这样做:

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

But this, 但是这个,
a) IMHO, isn't correct way a)恕我直言,这不是正确的方法
b) gives me error "You did not select a file to upload." b)给我错误“您没有选择要上传的文件”。
Update 更新资料
This is a way out I could apply: 这是我可以申请的出路:

        $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
            }
        }

检查以下内容: https : //github.com/nicdev/CodeIgniter-Multiple-File-Upload ,应该执行上面想要的操作。

in HTML5 you can use this functionality HTML5中,您可以使用此功能

If you want to let the user select multiple files, simply use the multiple attribute on the input element: 如果要让用户选择多个文件,只需在输入元素上使用multiple属性:

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

REFERENCE 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM