How to upload multiple images using codeigniter?

Member

by shyann , in category: PHP CMS , 2 months ago

How to upload multiple images using codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 2 months ago

@shyann 

To upload multiple images using CodeIgniter, you can follow these steps:

  1. Create a form in your view file where users can select multiple images to upload:
1
2
3
4
<form action="<?php echo base_url('upload/upload_images'); ?>" method="post" enctype="multipart/form-data">
    <input type="file" name="userfile[]" multiple>
    <input type="submit" value="Upload Images">
</form>


  1. Create a controller named Upload.php in your controllers directory:
 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
<?php
class Upload extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function upload_images() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 10000; // maximum size in KB

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

        if (!$this->upload->do_upload('userfile')) {
            $error = array('error' => $this->upload->display_errors());
            print_r($error);
        } else {
            $data = array('upload_data' => $this->upload->data());
            print_r($data);
        }
    }
}
?>


  1. Create an uploads directory in your root directory to store uploaded images.
  2. Make sure that the upload and fileinfo PHP extensions are enabled in your PHP configuration.
  3. Run your application and navigate to the form you created in Step 1. Select multiple images and click on the 'Upload Images' button to upload them.


This example demonstrates how to upload multiple images using CodeIgniter's file upload library. You can modify the code to suit your specific requirements, such as adding validation, resizing images, or saving image paths to a database.