Codeigniter multiple file upload messes file extension

后端 未结 5 1453
梦如初夏
梦如初夏 2020-11-29 22:41

I am trying to get a multiple upload library working for my Codeigniter based website. I have it working almost but I have a slight problem if I upload more than one image,

相关标签:
5条回答
  • 2020-11-29 23:01

    I was able to get multiple file uploads to work with clux's method but I had to modify the upload class slightly (I'm using Codeigniter 2). First I created a duplicate of the CI upload class into my applications library.

    Line 143:

    public function do_upload($field = 'userfile')
    

    change to

    public function do_upload($field = 'userfile', $i = 0)
    

    And between lines 160 and 200, you must add [$i] to the end of the $_FILES variables.

    Example:

    is_uploaded_file($_FILES[$field]['tmp_name'])
    

    change to:

    is_uploaded_file($_FILES[$field]['tmp_name'][$i])
    

    I believe there is a total of 9 of them.

    0 讨论(0)
  • 2020-11-29 23:04

    I solved this exact problem by amending the Upload.php files located in the library folder.

    Comment out line 935:

    $filename = $this->file_name;
    
    0 讨论(0)
  • 2020-11-29 23:10

    I am a bit confused by your answer thephpx. Won't restructuring $_FILES like that kill off the subarrays once you ahve run it once?

    At any rate, I was trying OP's approach by adding the extra index to the a few of the upload library's lines.

    I made it work by putting the for loop (over all files) in the upload controller, and the extra index $k as a parameter in the do_upload function in the library.

                $this->load->library('upload');
            for ($k = 0; $k < count($_FILES['userfile']['name']); $k++) {
                $this->upload->initialize($upload); //must reinitialize to get rid of your bug ( i had it as well)
                if (!$this->upload->do_upload('userfile',$k)) {
                    $this->load->view('upload/image_form', $data + array('error'=>$this->upload->display_errors()));
                }
                $udata[$k] = $this->upload->data(); //gradually build up upload->data()
            }
    
    0 讨论(0)
  • 2020-11-29 23:12

    For me it was enough to recall $this->upload->initialize($config); every time I called $this->upload->do_upload()

    0 讨论(0)
  • 2020-11-29 23:14

    Faced the exact similar stuff 2 days ago but did it the old fashioned way hope it helps. try this ..

    function tester(){
    
     $this->load->library('upload');  // NOTE: always load the library outside the loop
     $this->total_count_of_files = count($_FILES['filename']['name'])
     /*Because here we are adding the "$_FILES['userfile']['name']" which increases the count, and for next loop it raises an exception, And also If we have different types of fileuploads */
     for($i=0; $i<$this->total_count_of_files; $i++)
     {
    
       $_FILES['userfile']['name']    = $_FILES['filename']['name'][$i];
       $_FILES['userfile']['type']    = $_FILES['filename']['type'][$i];
       $_FILES['userfile']['tmp_name'] = $_FILES['filename']['tmp_name'][$i];
       $_FILES['userfile']['error']       = $_FILES['filename']['error'][$i];
       $_FILES['userfile']['size']    = $_FILES['filename']['size'][$i];
    
       $config['file_name']     = 'test_'.$i;
       $config['upload_path']   = './public/uploads/';
       $config['allowed_types'] = 'jpg|jpeg|gif|png';
       $config['max_size']      = '0';
       $config['overwrite']     = FALSE;
    
      $this->upload->initialize($config);
    
      if($this->upload->do_upload())
      {
        $error += 0;
      }else{
        $error += 1;
      }
     }
    
     if($error > 0){ return FALSE; }else{ return TRUE; }
    
    }
    
    0 讨论(0)
提交回复
热议问题