How to upload image in Codeigniter?

by elisha_langworth , in category: PHP Frameworks , 2 years ago

How to upload image in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by deron , a year ago

@elisha_langworth 

To upload an image in CodeIgniter, you will need to follow these steps:

  1. Create a form in your view file with an input element of type file and a submit button.
1
2
3
4
5
<form method="post" action="<?php echo base_url('upload/do_upload'); ?>" enctype="multipart/form-data">
    <input type="file" name="userfile" size="20" />
    <br /><br />
    <input type="submit" value="upload" />
</form>


  1. In your controller, create a method to handle the form submission. In this method, you will use the upload library to handle the file upload.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 100;
    $config['max_width'] = 1024;
    $config['max_height'] = 768;

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

    if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}


  1. In the $config array, specify the upload path, allowed file types, and other options such as maximum size and dimensions.
  2. Load the upload library and pass it the $config array.
  3. Use the do_upload method of the upload library to handle the actual file upload. If the upload is successful, the uploaded file data will be returned in the $data array. If the upload fails, an error message will be returned in the $error array.
  4. Depending on the result of the upload, you can either display an error message or a success message, along with any relevant data such as the uploaded file name or size.

Member

by shyann , 8 months ago

@elisha_langworth 

To upload an image in CodeIgniter, you can follow these steps:

  1. First, make sure you have set up the necessary configuration settings in the config.php and database.php files of your CodeIgniter project.
  2. Create a form in your view file (upload_form.php) with an input field of type file for the image upload:
1
2
3
4
    
    


  1. Create a controller method to handle the image upload. In the example above, the form's action points to image/upload_image, so create a new method upload_image() in your Image controller:
 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
27
28
29
30
31
32
33
34
35
36
// controllers/Image.php

class Image extends CI_Controller {
    public function upload_image() {
        // Check if file is selected for upload
        if (isset($_FILES['userfile']['name'])) {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = 2048; // 2MB

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

            if (!$this->upload->do_upload('userfile')) {
                // If upload fails, redirect to error page or show error message
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);
            } else {
                // If upload is successful, retrieve file data
                $data = $this->upload->data();

                // Your further processing logic here, save file to database, etc.
                // ...

                // Redirect to success page or show success message
                redirect('image/upload_success');
            }
        }
        
        // If no file is selected for upload, redirect to form page
        redirect('image');
    }

    public function upload_success() {
        $this->load->view('upload_success');
    }
}


  1. Create the upload_success.php view file to display a success message after the image upload:
1
Image uploaded successfully!


  1. Make sure you have created a uploads folder in the root directory of your CodeIgniter project to store the uploaded images. Ensure the folder has write permissions.


That's it! Now, when you submit the form, the selected image will be uploaded to the uploads folder and the user will be redirected to the upload_success page.