Upload file with database codeigniter

Upload file with database codeigniter

Today we are going to show you how to upload files into a folder and save it’s name in a database table in simple way. In an application, we uploading single image.
In codeigniter that’s the function responsible for performing the file upload operation you may want to read this: File uploading Class

Here we are explaining step by step how can we create simple effective codeigniter file uploading system.
This codeigniter file uploading system is tested and working fine in case if you getting any kind of errors and not understanding how to implement feel free and comment as soon as possible we will back to you.

  • Take one files as an input.
  • Once upload button is pressed, the file will be upload.
  • When the file is uploaded, an its name is inserted in the database table.

Step 1. Create view for uploading file in codeigniter with database

Create a view called fileuploading.php. in it copy and past below code and save it to your application/views/ directory:

<?php
/*
	Author: Mukesh Jakhar
	Description: File uploading with database (View)
*/
defined('BASEPATH') OR exit('No direct script access allowed');
$this->load->helper('url');
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>User registration form</title>
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
	<div class="col-md-3"></div>
		<div class="col-md-6">
			<?php if(isset($msg)){echo $msg;}?>
			<form method="post" enctype="multipart/form-data">
				<div class="form-group">
					<label>Upload File</label>
					<input class="form-control" type="file" name="user_image" />
				</div>
				<div class="form-group">
					<input class="btn btn-primary" type="submit" value="Upload File" />
				</div>
			</form>
		</div>
	<div class="col-md-3"></div>
</div>
</body>
</html>

Step 2. Create controller for uploading file in codeigniter with database

Create a controller called Upload.php. in it copy and past below code and save it to your application/controllers/ directory:

<?php
/*
	Author: Mukesh Jakhar
	Description: File uploading with database (Controller)
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Upload extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->model('File_uploading');
	}

	public function index()
	{
		$this->load->helper(array('form', 'url'));
		if($this->input->post('user_image'))
		{
			$config = array(
				'upload_path' => "./uploads/",
				'allowed_types' => "gif|jpg|png|jpeg|JPEG|JPG|PNG|GIF",
				'overwrite' => TRUE,
				'max_size' => "2048000",
				'max_height' => "768",
				'max_width' => "1024"
			);
			$this->load->library('upload', $config);

			$formData = array(
				'image' => $this->input->post('user_image')
			);

			if ( ! $this->upload->do_upload('user_image'))
			{
				$res = array('msg' => $this->upload->display_errors());
				$this->load->view('fileuploading', $res);
			}
			else
			{
				$res = array('msg' => "File has been uploaded!");
				$this->File_uploading->uploading($formData);
				$this->load->view('fileuploading', $res);
			}
		}
		else
		{
			$this->load->view('fileuploading');
		}
	}
}

Step 3. Create model for uploading file in codeigniter with database

Create a model called File_uploading.php. in it copy and past below code and save it to your application/models/ directory:

<?php
/*
	Author: Mukesh Jakhar
	Description: File uploading with database (Model)
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class File_uploading extends CI_Model{
	function __construct(){
		$this->load->database();
		parent::__construct();
	}
	function uploading($formData){
		$this->db->insert('users', $formData);
		// Insert file into databse table "users" field name "image".
	}
}
?>

Article information:

Article Name: Upload file with database codeigniter
Publish Date: 13-08-2018
Google Search Keywords: how to upload image in codeigniter with database, save uploaded image in database by using codeigniter, how to insert image in mysql database using codeigniter, codeigniter upload image to database example, file upload in codeigniter using database, how to insert image name in database using codeigniter, save image path in database codeigniter, how to upload image in php to database using codeigniter, image upload and display in codeigniter, how to upload image in codeigniter, codeigniter upload image and insert into database

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.