phpcodeignitercodeigniter-2

upload multiple file upload using codeigniter file name not working


I tried to upload multiple file upload using codeigniter 2.

I was working fine , but file name getting wrong. I tried to change file name every time i tried it was not working.

function do_upload($last_insert_id)
{  
    $files = $_FILES; 
    $cpt = count($_FILES['userfile']['name']);
    $file_name_array=array(); 
    for($i=0; $i<=$cpt; $i++)
    {  
        $config = array();
        if($files['userfile']['size'][$i] > 0 ) { 
            $image_name = "ledger_".$last_insert_id."_".$i;
            $config['upload_path'] ='./uploads/ledger/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|xsl|xlsx|xslx|csv|xls';
            $config['max_size']      = '0';
            $config['overwrite']     = FALSE; 
            $config['file_name'] = $image_name;  
            $this->load->library('upload', $config); 
    
            $_FILES['userfile']['name']= $files['userfile']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile']['size'][$i];  
           // $this->upload->initialize($this->set_upload_options());
           
            //$this->upload->do_upload();
            if($this->upload->do_upload()) {
                echo $image_name;           
                array_push($file_name_array,$image_name);
             
            } else {
                $empty_file='';
                array_push($file_name_array,$empty_file);
            }
        } 

    }
    echo "<pre>";
                print_r( $file_name_array);
}

Solution

  • I found the solution

    Here it is

    function do_upload($last_insert_id)
    {  
        $files = $_FILES; 
        $cpt = count($_FILES['userfile']['name']);
        $file_name_array=array(); 
        for($i=0; $i < $cpt; $i++)
        {  
     
            $config = array();
            if($files['userfile']['size'][$i] > 0 ) 
            {  
            $image_name = "ledger_".$last_insert_id."_".$i;
    
            $config['file_name'] = $image_name; 
            $config['upload_path'] ='./uploads/ledger/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|xsl|xlsx|xslx|csv|xls';
            $config['max_size']      = '0';
            $config['overwrite']     = FALSE; 
    
            $this->load->library('upload', $config); 
    
            $_FILES['userfile']['name']= $files['userfile']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile']['size'][$i];  
     
            //$this->upload->do_upload();
            if($this->upload->do_upload()) { 
                $fileData = $this->upload->data(); 
                $filenameData = $fileData['file_name'];
                //print_r($fileData);
                array_push($file_name_array,$filenameData);
             
            }
            }
             else { 
                $empty_file='';
                array_push($file_name_array,$empty_file);
            } 
      
        }
         return $file_name_array;
    

    }