Server Side Validation on File Upload Using Codeigniter

Kailash Singh
Server Side Validation on File Upload Using Codeigniter
Published Nov 13,2019 by Kailash Singh

0 Comment     782 Views    


In this tutorial, we are going to learn about how to create Server Side Validation on File Upload Using Codeigniter.

 

First of all we will create a HTML From then we will work codeigniter validation.

Feature: 

  • Check file field is not empty.
  • If the file extension is one of .jpg, .png.
  • If the file size is less than or 3MB.

 

Step 1 :- Create View in View Folder (website.php)

 

 The following HTML code below creates an uploader form. This form is having method attribute set to action, post and enctype attribute is set to multipart/form-data

 

<form action="<?php echo base_url(); ?>welcome/save" method="post" enctype="multipart/form-data">  	
	  <input type="file" class="form-control" name="file"><br>
	  <button name="submit" type="submit" class="btn btn-primary">Submit</button>
</form>

 

Step 2 :- Create Upload Image Function in Website.php

 

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

class Welcome extends CI_Controller {

	public function index()
	{
		$this->load->view('welcome_message');  // Load Html File
	}

	public function save()
	{
		$filename=$_FILES['file']['name'];

		$ext = pathinfo($filename, PATHINFO_EXTENSION); // get extension

		$filesize="3145728"; // Define maximum size in byte (3MB)

		$sizesign = $_FILES['file']['size']; //checking size
		
               //file empty or not
		if($filename != '') 
		{
            
                          //check extension validation
			if($ext != 'jpg' && $ext != 'jpeg' && $ext != 'JPG' && $ext != 'png' && $ext != 'PNG')
			{
				$msg='<div class="alert alert-danger alert-dismissible">
					  <button type="button" class="close" data-dismiss="alert">&times;</button>
							<strong>Failed!</strong> Only jpg & png files allowed.
					</div>';
				$this->session->set_flashdata('flashmsg',$msg);
				return redirect('welcome');
				exit();
			}
			else if($sizesign > $filesize)  
			{
                                // check file size validation
				$msg='<div class="alert alert-danger alert-dismissible">
						<button type="button" class="close" data-dismiss="alert">&times;</button>
							<strong>Failed!</strong> Please upload less than 3MB file.
						</div>';
				$this->session->set_flashdata('flashmsg',$msg);
				return redirect('welcome');
				exit();
			}
			else{

                              //when all validation has been done
				$msg='<div class="alert alert-success alert-dismissible">
						<button type="button" class="close" data-dismiss="alert">&times;</button>
							<strong>Success!</strong> Data submitted successfully
						</div>';
				$this->session->set_flashdata('flashmsg',$msg);
				return redirect('welcome');
			}
			
		}
		else{
				$msg='<div class="alert alert-danger alert-dismissible">
						<button type="button" class="close" data-dismiss="alert">&times;</button>
							<strong>Failed!</strong> Please upload the file.
					</div>';
				$this->session->set_flashdata('flashmsg',$msg);
				return redirect('welcome');
		}
		
	}


}

 

Hope this will help our developers.

Because we believe Mantra means CodingMantra

Comments ( 0 )