Search code examples
phpcodeigniterfile-uploadcodeigniter-2codeigniter-3

Multiple File Upload Controls not working in CI


In CodeIgniter, I have 4 file upload controls as under:

<?php echo form_open_multipart('uploadimg');?>
<input type="file" name="image1" />
<input type="file" name="image2" />
<input type="file" name="image3" />
<input type="file" name="image4" />
<input type="submit" value="upload" />
<?php echo form_close();?>

Following is my file upload code:

$name1="img1.jpg";
$config1=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name1,
    );
$this->load->library('upload',$config1);
if($this->upload->do_upload('image1'))
{
    echo "Image 1 has been uploaded successfully";
}
else
{
    echo $this->upload->display_errors();
}
$name2="img2.jpg";
$config2=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name2,
    );
$this->load->library('upload',$config2);
if($this->upload->do_upload('image2'))
{
    echo "Image 2 has been uploaded successfully";
}
else
{
    echo $this->upload->display_errors();
}
$name3="img3.jpg";
$config3=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name3,
    );
$this->load->library('upload',$config3);
if($this->upload->do_upload('image3'))
{
    echo "Image 3 has been uploaded successfully";
}
else
{
    echo $this->upload->display_errors();
}
$name4="img4.jpg";
$config4=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name4,
    );
$this->load->library('upload',$config4);
if($this->upload->do_upload('image4'))
{
    echo "Image 4 has been uploaded successfully";
}
else
{
  echo $this->upload->display_errors();
}

I am getting the following output:


Image 1 has been uploaded successfullyImage 2 has been uploaded successfullyImage 3 has been uploaded successfullyImage 4 has been uploaded successfully


But, in the uploads folder, I am getting only 1 file. Strange thing is that the file that is being shown is of last file upload control, but the name of file is img1., i.e. name of first uploaded file.

Why is this happening? What is the solution?


Solution

  • Set each config using the initialize() method and not when loading the upload library.

    Changed:

    $this->load->library('upload', $config*);
    

    To:

    $this->upload->initialize($config*);
    

    Updated code:

    $this->load->library('upload');
    
    $name1   = "img1.jpg";
    $config1 = array(
        'upload_path' => FCPATH . "uploads/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
        'overwrite' => TRUE,
        'max_size' => "500",
        'file_name' => $name1
    );
    
    $this->upload->initialize($config1);
    if ($this->upload->do_upload('image1')) {
        echo "Image 1 has been uploaded successfully";
    } else {
        echo $this->upload->display_errors();
    }
    $name2   = "img2.jpg";
    $config2 = array(
        'upload_path' => FCPATH . "uploads/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
        'overwrite' => TRUE,
        'max_size' => "500",
        'file_name' => $name2
    );
    $this->upload->initialize($config2);
    
    if ($this->upload->do_upload('image2')) {
        echo "Image 2 has been uploaded successfully";
    } else {
        echo $this->upload->display_errors();
    }
    $name3   = "img3.jpg";
    $config3 = array(
        'upload_path' => FCPATH . "uploads/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
        'overwrite' => TRUE,
        'max_size' => "500",
        'file_name' => $name3
    );
    $this->upload->initialize($config3);
    
    if ($this->upload->do_upload('image3')) {
        echo "Image 3 has been uploaded successfully";
    } else {
        echo $this->upload->display_errors();
    }
    $name4   = "img4.jpg";
    $config4 = array(
        'upload_path' => FCPATH . "uploads/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
        'overwrite' => TRUE,
        'max_size' => "500",
        'file_name' => $name4
    );
    $this->upload->initialize($config4);
    if ($this->upload->do_upload('image4')) {
        echo "Image 4 has been uploaded successfully";
    } else {
        echo $this->upload->display_errors();
    }