Codeigniter & HTML5 - Trying to Upload Multiple Images at Once

后端 未结 3 914
难免孤独
难免孤独 2020-12-09 13:59

View Looks Like This


        

        
相关标签:
3条回答
  • 2020-12-09 14:33

    The loop you need:

    for($i=0; $i<count($_FILES['name']); $i++){
        if ($_FILES['error'][$i] == 0) {
            //do your stuff here, each image is at $_FILES['tmp_name'][$i]
        }
    }
    

    Note that is doesn't use the CI upload class, but plain PHP, which I usually find easier to work with instead of the CI's class.

    0 讨论(0)
  • 2020-12-09 14:44

    Check out this class for multiple file upload https://github.com/stvnthomas/CodeIgniter-Multi-Upload

    This did the trick for me.

    0 讨论(0)
  • 2020-12-09 14:55

    I ran into this problem aswell. The $_FILES data is sent a diffent structure (due to the multiple="" attribute) so codeigniter can't handle it. Prepend this before the uploading process:

    $arr_files  =   @$_FILES['userfile'];
    
    $_FILES     =   array();
    foreach(array_keys($arr_files['name']) as $h)
    $_FILES["file_{$h}"]    =   array(  'name'      =>  $arr_files['name'][$h],
                                        'type'      =>  $arr_files['type'][$h],
                                        'tmp_name'  =>  $arr_files['tmp_name'][$h],
                                        'error'     =>  $arr_files['error'][$h],
                                        'size'      =>  $arr_files['size'][$h]);
    

    Then in the loop function use this:

    $this->load->library('upload');
    
    $arr_config =   array(  'allowed_types' =>  'gif|jpg|png',
                                'upload_path'   =>  'url_path/');
    
    foreach(array_keys($_FILES) as $h) {
    
        // Initiate config on upload library etc.
    
        $this->upload->initialize($arr_config);
    
        if ($this->upload->do_upload($h)) {
    
            $arr_file_data  =   $this->upload->data();
    
        }
    
    }
    

    Explanation:
    I simply change the structure of $_FILES to the common structure which is sent on a default <input type="file" /> and the run a loop, fetching all their key names.

    0 讨论(0)
提交回复
热议问题