Codeigniter Image Upload with Validation Example

By Hardik Savani November 5, 2023 Category : PHP Codeigniter

In this post, we learn step by step codeigniter image uploading from scratch. Image Upload functionality is a very basic require to all most back-end project for like user profile picture upload, product image upload, category photo upload etc.

So in this tutorial, i am going to share with you small example of image uploading with validation from scratch. So you can simply identify how upload image or file with validation, we can simply use validation for mime type, file max size etc. In this example i will use "upload" library of Codeigniter. codeigniter provide pre-define upload library that way we can simple use with image upload or file upload. In this example i also use "form" helper and "url" helper.

So, let's follow few bellow step to make image uploading example.

Step 1: Download Fresh Codeigniter 3

In First step we will download fresh version of Codeigniter 3, so if you haven't download yet then download from here : Download Codeigniter 3.

Step 2: Add Route

In this step you have to add some route in your route file. So first we will create route for image uploading example.so put the bellow content in route file:

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['image-upload'] = 'ImageUpload';

$route['image-upload/post']['post'] = "ImageUpload/uploadImage";

Step 3: Create Controller

In this step, we have to create "ImageUpload" controller with index() and uploadImage(). so create Items.php file in this path application/controllers/Items.php and put bellow code in this file:

application/controllers/ImageUpload.php

<?php


class ImageUpload extends CI_Controller {


/**

* Manage __construct

*

* @return Response

*/

public function __construct() {

parent::__construct();

$this->load->helper(array('form', 'url'));

}


/**

* Manage index

*

* @return Response

*/

public function index() {

$this->load->view('imageUploadForm', array('error' => '' ));

}


/**

* Manage uploadImage

*

* @return Response

*/

public function uploadImage() {


$config['upload_path'] = './uploads/';

$config['allowed_types'] = 'gif|jpg|png';

$config['max_size'] = 1024;

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


if ( ! $this->upload->do_upload('image')) {

$error = array('error' => $this->upload->display_errors());

$this->load->view('imageUploadForm', $error);

}else {

print_r('Image Uploaded Successfully.');

exit;

}

}


}


?>

Step 4: Create View

In this step we will create imageUploadForm.php view file . In this file we will write design of html form using form helper and url helper. So let's update following file:

application/views/imageUploadForm.php

<!DOCTYPE html>

<html>

<head>

<title>Image Upload Example from Scratch</title>

</head>


<body>


<?php echo $error;?>

<?php echo form_open_multipart('image-upload/post');?>

<input type="file" name="image" size="20" />

<input type="submit" value="upload" />

</form>


</body>

</html>

Ok Now we are ready to run Above example, But We have to create "uploads" folder on your root directory before run example. all images will upload on "uploads" directory so make sure permission too.

So let's run bellow command on your root directory for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:

http://localhost:8000/image-upload

Make Sure, First you have to set base URL on your bellow configuration file :

application/config/config.php

$config['base_url'] = 'http://localhost:8000/';

I hope it can help you...

Shares