How to Upload Image And File in CodeIgniter

Upload Image And File In CodeIgniter
Share This Post

Upload Image And File In CodeIgniter ? Are You Looking For ?Here We Are To Solve Your Doubt. In this tutorial, we will look at how to use the file upload library to load filesUploading Images in CodeIgniter.

CodeIgniter File Upload

File Management is most important in front-end an back-end as well. if you are developing conent management system, then you definitely need to upload images, word document, pdf, etc. so without wasting your time, lets start this article.

Upload image And File in CodeIgniter

File Or Image Upload in CodeIgniter is Done in two parts. Front-end And Back-end. So Basically Front-End just give file to Back-end. and back-end will upload file into your server. there are existing library for image upload in codeigniter. So Its Too Easy to upload file in codeigniter. follow below step. Here is the official codeIgniter library.

1. Make new Directory in your CodeIgniter Project Root Folder. All Files will be save in this directory.

2. Creating the Upload Form.
– Using a text editor, create a form called upload_form.php. In it, place this code and save it to your application/views/ directory:

Using a text editor, create a form called upload_success.php. In it, place this code and save it to your application/views/ directory:

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<h3>Your file was successfully uploaded!</h3>

<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>

<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>

</body>
</html>

The Controller

Using a text editor, create a controller called Upload.php. In it, place this code and save it to your application/controllers/ directory:

<?php

class Upload extends CI_Controller {

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

        public function index()
        {
                $this->load->view('upload_form', array('error' => ' ' ));
        }

        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);
                }
        }
}
?>

The Upload Directory

You’ll need a destination directory for your uploaded images. Create a directory at the root of your CodeIgniter installation called uploads and set its file permissions to 777.

Try it!

To try your form, visit your site using a URL similar to this one:

example.com/index.php/upload/

You should see an upload form. Try uploading an image file (either a jpg, gif, or png). If the path in your controller is correct it should work.

Initializing the Upload Class

Like most other classes in CodeIgniter, the Upload class is initialized in your controller using the $this->load->library() method:

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

Once the Upload class is loaded, the object will be available using: $this->upload

Setting Preferences

Similar to other libraries, you’ll control what is allowed to be upload based on your preferences. In the controller you built above you set the following preferences:

$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);

// Alternately you can set preferences by calling the ``initialize()`` method. 
Useful if you auto-load the class:
$this->upload->initialize($config);

The above preferences should be fairly self-explanatory. Below is a table describing all available preferences.

In $config[‘allowed_types’] = ‘gif|jpg|png’; you can use custom file or like that. suppose if you want to upload mp3 file then you can write mp3 there. it is like file validation, so basically this is working as validator. if you want to allloe all files then simply write * there.

Here is Official Codeigniter Documentation

How to Upload Image And File in CodeIgniter

Also Read Our Flutter Tutorial

So This is Article On How to upload image and File in CodeIgniter. Share This Article And Comment Below if you have issue.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

close