How to upload image in codeigniter

Leave a Comment
Hello guy's, Today we gonna look how to upload image with codeigniter. In this tutorial we will use CodeIgniter's File Uploading Class which help to upload file. Using this class we can also set various preferences, restricting the type and size of the files.

Creating the Upload Form i.e. image_upload.php (View)

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

<?php echo form_open_multipart('image_upload/do_upload');?>
<input type="file" name="profile_pic" size="20" />
<br /><br />
<input type="submit" value="upload" name="submit_form"/>
</form>
<?php if($error) { echo ($error['error']); }?>

</body>
</html>
You'll notice we are using a form helper to create the opening form tag. File uploads require a multipart form, so the helper creates the proper syntax for you. You'll also notice we have an $error variable. This is so we can show error messages in the event the user does something wrong.

image_upload.php (Controller)

Using a text editor, create a controller called image_upload.php. In it, place this code and save it to your applications/controllers/ folder:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Image_upload extends CI_Controller {

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

public function index()
{
 $arrData['error'] = '';
 $arrData['content'] = 'image_upload';
 $this->load->view('template',$arrData);
}

public function do_upload(){
 if($this->input->post('submit_form')){
  $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('profile_pic'))
  {
                    $error = array('error' => $this->upload->display_errors());
   $arrData['error'] = $error;
   $arrData['content'] = 'image_upload';
   $this->load->view('template',$arrData);
  }
  else
  {
          $arrData['upload_data'] = $this->upload->data();
   $arrData['content'] = 'upload_success';
   $this->load->view('template',$arrData);
  }
 }
}
}
Now create uploads upload folder in root of your CodeIgniter installation and set its file permissions to 777.Now the last thing creating upload_success view

Creating upload Success view i.e upload_success.php (view)

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

<h3>Your file was successfully uploaded!</h3>
<ul>
<?php echo "<pre>"; print_r($upload_data); ?>
</ul>
<p><?php echo anchor('image_upload', 'Upload Another File!'); ?></p>
</body>
</html>
To try your form, visit your http://localhost/Proejct_foldername/image_upload. If you did't remove your index.php from url then you have to try with index.php in your url. Learn Here how to remove index.php from codeigniter Try uploading an image file (either a jpg, gif, or png). If the path in your controller is correct it should work. After successfully upload image you will get below output on upload_success page.
How to upload image in codeigniter

0 comments:

Post a Comment

Powered by Blogger.