How to upload 2 separate images in codeigniter?

by elisha_langworth , in category: PHP Frameworks , a year ago

How to upload 2 separate images in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 6 months ago

@elisha_langworth 

To upload 2 separate images in CodeIgniter, you can follow these steps:

  1. Create a form in your view file with two file input fields for selecting the images to upload:
1
2
3
4
5
<form method="post" action="<?php echo base_url('upload/upload_images'); ?>" enctype="multipart/form-data">
    <input type="file" name="image1">
    <input type="file" name="image2">
    <button type="submit">Upload Images</button>
</form>


  1. Create an Upload controller with a method to handle the image upload:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Upload extends CI_Controller {

    public function upload_images() {
        // Set upload configuration
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);

        // Upload image1
        if ($this->upload->do_upload('image1')) {
            $image1_data = $this->upload->data();
            $image1_path = $image1_data['file_name'];
        }

        // Upload image2
        if ($this->upload->do_upload('image2')) {
            $image2_data = $this->upload->data();
            $image2_path = $image2_data['file_name'];
        }

        // Save the file paths to the database or do something else with them
    }
}


  1. Make sure the uploads directory is writable so that the uploaded images can be stored in it.


By following these steps, you should be able to upload 2 separate images in CodeIgniter.