1

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?

2

3 Answers 3

2

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, can we FCPATH with base_url() in this line: 'upload_path' => FCPATH . "c/uploads/",? for me $_SERVER['DOCUMENT_ROOT'] in place of FCPATH is working for me, but base_url() is not working
Preferably FCPATH. If 'c/' is the dir where your codeigniter resides, you can just omit 'c/' in - 'upload_path' => FCPATH . "uploads/"
It's fine if you use $_SERVER['DOCUMENT_ROOT']. FCPATH is more flexible in this case tho.
base_url() is for url, not for directory path. So it won't work in this case.
0

You may try this. This is working properly to upload two file (audio, image) or more file to the database.

Table Name : products

| id | product_name | price | description| product_image | audio_file |

HTML

<form name="addProduct" id="addProduct" method="POST" enctype="multipart/form-data">
    <input type="text" name="product_name" placeholder="Product Name">
    <input type="text" name="price" placeholder="Product Price">
    <textarea id="description" name="description" class="materialize-textarea" placeholder="Description"></textarea>
    <input type="file" name="product_image" id="fileImg">
    <input type="file" name="audio_file" id="audio_file">
    <input type="button" name="addProduct" value="Add Product" class="btn btn-flat btn-add-product">
</form>

Controller

public function addProducts(){
/*Initialization of model*/
$this->load->model("product_model");

/*store data into array*/
$result=array(
        "product_name"=>$_POST["product_name"],
        "price"=>$_POST["price"],
        "description"=>$_POST["description"],
        );

$proID = $this->product_model->addProduct($result); //sending data to model for insertion

//Define the file names with blog id with same extension which has been uploaded
$product_image = $proID."_product.".pathinfo($_FILES['product_image']['name'], PATHINFO_EXTENSION);     
$product_audio = $proID."_audio.".pathinfo($_FILES['audio_file']['name'], PATHINFO_EXTENSION);      

$updateData = array(
    "product_image" => $product_image,
    "audio_file"=>$product_audio            
);

// update the name of the images in the database
$this->product_model->updateProduct($updateData,$proID);

//set configuration for the upload library

/* |===> Image Config  <==|*/
$config['upload_path'] = 'html/images/products/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;

/* |==> Audio Config <==|*/
$config_audio['upload_path'] = 'html/images/products/audios/';
$config_audio['allowed_types'] = 'mp3|mp4';
$config_audio['overwrite'] = TRUE;
$config_audio['encrypt_name'] = FALSE;
$config_audio['remove_spaces'] = TRUE;

//set name in the config file for the feature image
$config['file_name'] = $proID."_product";

$this->load->library('upload', $config);  // config first file 
$this->upload->do_upload('product_image');
$this->upload->display_errors();

$config_audio['file_name'] = $proID."_audio";
$this->upload->initialize($config_audio); // config seconf file

$this->upload->do_upload('audio_file');
$this->upload->display_errors();

}

Model

/*Add Product*/
public function addProduct($pro_data){
    $query=$this->db->insert("products",$pro_data);       
    $id=$this->db->insert_id();
    return $id;
}

/*Update for Multiple File Upload*/
public function updateProduct($result,$up_id){
    $this->db->where('id',$up_id);
    $this->db->update("products",$result);
}

Comments

0

This is working nicely to my website and i hope it will help you to boost your code.

HTML

<form class="col s12" id='propertyform' enctype="multipart/form-data">
 <input type="file" name="image1" />
 <input type="file" name="image2" />
 <input type="file" name="image3" />
 <input type="file" name="image4" />
<a class="waves-effect waves-light btn addproperty">add</a>
</form>

js

$(".addproperty").on("click",function()
{
    var baseurl = $("#base_url").val();
    var property = new FormData($("#propertyform")[0]);
    $.ajax({
          url : baseurl+"dropdowns/add_propertyy",
          data : property,
          type:"POST",
          contentType:false,
          processData:false,
          success:function(res)
          {
            window.location.reload();
          }
    }); 
});

controller

public function add_propertyy()
{
  $id = 1; 
  $images = $id."_image1.".pathinfo($_FILES['image1']['name'], PATHINFO_EXTENSION);
  $images1 = $id."_image2.".pathinfo($_FILES['image2']['name'], PATHINFO_EXTENSION);
  $images2 = $id."_image3.".pathinfo($_FILES['images3']['name'], PATHINFO_EXTENSION);
  $images3 = $id."_image4.".pathinfo($_FILES['image4']['name'], PATHINFO_EXTENSION);

  $addimg = array(
    "images1" =>  $images,
    "images2" =>  $images1,
    "images3" =>  $images2,
    "images4" =>  $images3,     
   );

  $this->load->model('property_model');
  $this->property_model->add_pro_img($addimg,$id);


    $uploadPath = "./html/images/property";

    $config['upload_path'] = $uploadPath;
    $config['allowed_types'] = 'gif|jpg|png|pdf';
    $config['overwrite'] = TRUE;
    $config['encrypt_name'] = FALSE;
    $config['remove_spaces'] = TRUE;

    $config['file_name'] = $id."_images1";
    $this->load->library('upload', $config);
    $this->upload->do_upload('images');
    $this->upload->display_errors();

    $config['file_name'] = $id."_images2";
    $this->upload->initialize($config);
    $this->upload->do_upload('images2');
    $this->upload->display_errors();       

    $config['file_name'] = $id."_images3";
    $this->upload->initialize($config);
    $this->upload->do_upload('images3');
    $this->upload->display_errors();

    $config['file_name'] = $id."_images4";
    $this->upload->initialize($config);
    $this->upload->do_upload('images4');
    $this->upload->display_errors();

 }

modal

public function add_pro($data) {
    $this->db->insert("property",$data);
    $id = $this->db->insert_id();
    return $id;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.