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

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

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') OR exit('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

class UploadImage extends CI_Controller {

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

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


   /**
    * Method to upload image to the server
    *
    * @return Response
   */
   public function uploadImage() { 


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

  <?php echo $error;?> 
  <?php echo form_open_multipart('upload-image/post');?> 
     <input type="file" name="image" />
     <input type="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

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort