How to upload image with validation in PHP Codeigniter with example?

How to upload image with validation in PHP Codeigniter with example?

In this post we will give you information about How to upload image with validation in PHP Codeigniter with example?. Hear we will give you detail about How to upload image with validation in PHP Codeigniter with example?And how to use it also give you demo for it if it is necessary.

In this PHP Codeigniter Tutorial, I am going to tell you how to upload image on the server and also you will know how to add validation rule to file upload in Codeigniter.

I need a multipart form to upload file and in Codeigniter, There is a helper method form_open_multipart to open multipart form tag.

This is one of most common requirements for all the web application to have a file upload functionality.

For this example, I will have a simple HTML form with input field and a submit button as usually so that I can select the file and There are number of libraries with Codeigniter that reduce the development time and I am going to use upload library to upload the files very easily on the server.


Routes

In this first step, I will add routes in our route file for uploading image.


application/config/routes.php

<?php
defined('BASEPATH') ORexit('No direct script access allowed');


$route['default_controller'] ='welcome';
$route['404_override'] ='';
$route['translate_uri_dashes'] =FALSE;
$route['upload-image'] ='UploadImage';
$route['upload-image/post']['post'] ="UploadImage/uploadImage";



Controller (UploadImage.php)

In this step, I will create a controller “UploadImage.php” and place the following code in it and save this file to application/controllers/ directory.


application/controllers/UploadImage.php

<?php

classUploadImageextends CI_Controller {

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

 
   publicfunctionindex() { 
      $this->load->view('imageupload_view', array('error'=>'' )); 
   } 


   /**
    * Method to upload image to the server
    *
    * @return Response
   */
   publicfunctionuploadImage() { 


      $config['upload_path']   ='uploads/images/'; 
      $config['allowed_types'] ='gif|jpeg|jpg|png'; 
      $config['max_size']      =2048;
      $this->load->library('upload', $config);

      if ( !$this->upload->do_upload('image')) {
         $error=array('error'=>$this->upload->display_errors()); 
         $this->load->view('imageupload_view', $error); 
      }else { 
         echo"Upload Successful";
         exit;
      } 
   }

} 


?>


View (imageupload_view.php)

In this step, I will create a view imageupload_view.php file inside the application/views/ directory.


application/views/imageupload_view.php

<!DOCTYPE html>
<html> 
<head> 
  <title>Image upload in Codeigniter with example</title> 
</head>
<body> 

  <?phpecho$error;?> 
  <?phpecho form_open_multipart('upload-image/post');?> 
     <inputtype="file"name="image"/>
     <inputtype="submit"name="submit"value="Upload"/> 
  </form> 


</body>
</html>

  1. Codeigniter – Upload image with preview using jQuery Ajax Form Plugin with example
  2. Ajax Image Upload using PHP and jQuery Example Without Page Refresh with Validation
  3. Laravel PHP : Upload image with validation using jquery ajax form plugin
  4. Upload Multiple Images With Image Preview Using jQuery,Ajax And PHP

Try this..

Hope this code and post will helped you for implement How to upload image with validation in PHP Codeigniter with example?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

47  +    =  54

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US