Upload multiple images in Codeigniter

First of all, you must create a table images in your database

Follow  the following code in your controllers

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

    foreach ($_FILES['picture']['name'] as $key => $value) {
      $secure_id = get_secure_id($id).rand(1, 100);

      $_FILES['picture_'.$key]['name'] = $_FILES['picture']['name'][$key];
      $_FILES['picture_'.$key]['type'] = $_FILES['picture']['type'][$key];
      $_FILES['picture_'.$key]['tmp_name'] = $_FILES['picture']['tmp_name'][$key];
      $_FILES['picture_'.$key]['error'] = $_FILES['picture']['error'][$key];
      $_FILES['picture_'.$key]['size'] = $_FILES['picture']['size'][$key];

  if (!empty($_FILES['picture']['tmp_name'][$key]))
  {
        // set upload directory. create if not exist

        $org_directory = PATH_IMAGE.$this->session->vendor_id;

        if (!file_exists($org_directory)) {
          mkdir($org_directory, 0755, true);
        }

        // 1. upload file
        $file_name = $org_directory.$secure_id.'.jpg';

        // if file exist, delete
if (file_exists($file_name)) {
unlink($file_name);
}

        $config['upload_path'] = $org_directory;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = $secure_id.'.jpg';

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

//do upload
if ($this->upload->do_upload('picture_'.$key))
{
$image_data = $this->upload->data();

          // image manipulation
$config2['image_library'] = 'gd2';
$config2['source_image']  = PATH_IMAGE.$this->session->vendor_id.'/'
.$image_data['raw_name'].'.jpg';
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 800;

$this->load->library('image_lib', $config2);
          $this->image_lib->resize();
          $this->image_lib->clear();

$fields['image'] = $this->session->vendor_id.'/'.$image_data['raw_name'].
'.jpg';
  }
else
{
$fields['image'] = NULL;
        }
        
          $data_array = array(
            'invoice_id' => $id,
            'image' => $fields['image'],
          );
          $this->db->insert('images', $data_array);
      }
}

Make a looping as above so that you can upload multiple images.


For more details, you can go to https://www.codexworld.com/codeigniter-upload-multiple-files-images/












































Comments

Popular posts from this blog

What is getnada?