Image Upload In CodeIgniter

Image Upload In CodeIgniter
Published Mar 08,2019 by Kailash Singh
1 Comment 1340 Views
In this tutorial, we are going to learn about creating a Image Upload in CodeIgniter using Php Script.
A PHP script can be used with a HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a PHP script.
Step 1 :- Create table in database.
CREATE TABLE `codingmantra`.`upload_file` ( `id` INT(11) NOT NULL AUTO_INCREMENT , `image` VARCHAR(200) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
Step 2 :- Create Image Upload Page in View Folder (image_upload.php)
<form action="" method="">
<input type="file" name="upload">
<input type="submit" name="submit" value="Upload">
</form>
Step 3 :- Create Upload Image Controller in Controller Folder (Upload_image.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_Image extends CI_Controller {
public function index()
{
$this->load->view('image_upload');
}
}
Step 4 :- Create Action, Method In Image Upload View
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(); ?>upload_image/add_upload_image" method="post" enctype="multipart/form-data">
Step 5 :- Create Upload Image folder to store images
Step 6 :- Create Upload Image Function in Image Upload Controller
public function add_upload_image()
{
$new_image_upload='';
$image_upload = $_FILES['upload']['name'];
if($image_upload != '')
{
$image_upload1 = @end(explode('.', $image_upload));
$image_upload2 = strtolower($image_upload1);
$new_image_upload = time().'.'.$image_upload2;
$image_upload_path = './upload_image/'.$new_image_upload;
move_uploaded_file($_FILES['upload']['tmp_name'], $image_upload_path);
}
$data = array(
'image' => $new_image_upload
);
$this->load->model('upload_image_model');
$this->upload_image_model->upload_file_model($data);
return redirect('upload_image');
}
Step 7 :- Create Model in Model Folder (Upload_image_model.php).
<?php
class Upload_image_model extends CI_Model{
public function upload_file_model($data){
return $this->db->insert('upload_file',$data);
}
}
Hope this will help our developers.
SEARCH POST HERE
Support Us
Subscribe My YouTube Channel
Join Our Telegram Channel & Support Eachother
CATEGORIES
INTERVIEW QUESTIONS
PROJECT SOURCE CODE
POPULAR POSTS
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date