CodeIgniter Forums
Multiple images and data upload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Multiple images and data upload (/showthread.php?tid=64520)



Multiple images and data upload - azharsaleem6 - 02-29-2016

i'm beginner in codeigniter i want to upload multiple images and data upload and save images name in database. i don't understand how to store multiple files name in database. And upload images in my root folder.

Here is my code

View:

<form method="post" enctype="multipart/form-data" action="<?=base_url();?>vendors/home/do_upload">
    
                    <input type="text" name="product_name" class="form-control" required>

                    <input type="text" name="product_price" class="form-control" required>

                    <input type="text" name="old_price" class="form-control" required>

                    <input type="file" name="image1" required>

                    <input type="file" name="image2" required>

                    <input type="file" name="image3" required>

                    <input type="file" name="image4" required>

      </form>


Controller:

public function do_upload()
{

if ($_POST) {

$config['upload_path'] = './uploads/';
$config['max_size'] = 90000;
$config['max_width'] = 50000;
$config['max_height'] = 50000;
$config['allowed_types'] = 'jpg|png|gif|jpeg';

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

if (!$this->upload->do_upload()) {

$error = array('error' => $this->upload->display_errors());
$this->load->view('vendors/add_product',$error);

}


else{

$query = $this->upload->data();

$data = array(
          'pro_name' => $_POST['product_name'],
           'prod_price' => $_POST['product_price'], 
           'old_price' => $_POST['old_price'],  
            'prod_image1' => [' '],
            'prod_image2' => [' '],
            'prod_image3' => [' '],
            'prod_image4' => [' '],
 
);

$this->venmod->add_product($data);
redirect(base_url().'vendors/');
}
$this->load->view('footer');
}
}



Model:

public function add_product($data)

{
      $this->db->insert('vendor_products',$data);
}


RE: Multiple images and data upload - Wouter60 - 02-29-2016

You need an extra library. Here is an excellent one: https://github.com/avenirer/MY_Upload


RE: Multiple images and data upload - azharsaleem6 - 02-29-2016

Thansk Wouter60
But how i save multiple images name in my database ?


RE: Multiple images and data upload - Mangetsu - 03-01-2016

If you have fixed number of images just have one column in table for each. If you may have variable number of images, or want to save additional data for each image (extension, size etc) you may think about having images table connected to products by product_id key.


RE: Multiple images and data upload - Wouter60 - 03-01-2016

After uploading the files with the library, you can call $this->upload->data();
If you do this:
PHP Code:
$uploaded $this->upload->data(); 
then you have an array containing the metadata of all your uploaded files.

Now, let's assume you have a table in your database, called "uploads" with the fields: id, file_name, file_path, file_size, is_image, image_width, image_height. To store the data of each file as a record in your table, do this:

PHP Code:
foreach ($uploaded as $file) {
 
 $data = array(
 
   'file_name' => $file['file_name'],
 
   'file_path' => $file['file_path'],
 
   'file_size' => $file['file_size'],
 
   'is_image' => $file['is_image'],
 
   'image_width' => $file['image_width'],
 
   'image_height' => $file['image_height']
 
 );
 
 $this->db->insert('uploads',$data);


For a complete list of all file attributes that are in $this->upload->data(), check this page: http://www.codeigniter.com/userguide3/libraries/file_uploading.html