In this post we have share how to upload image and create multiple thumbnail sizes using CodeIgniter with MySQL. File upload functionality is one of the most common requirements for most of the web applications. Codeigniter file upload library is a very simple to use. File Uploading step-by-step instructions on how to upload file using CodeIgniter with MySQL.
Step 1: Create MySQL Database and Table
Step 2: Create a model file
Create a model file named "Upload_model.php" inside "application/models" folder.
Step 3: Update constants file
Add code the file application/config/constants.php in your CodeIgniter installation with you controller’s name.
Step 4: Create a controller file
Next create a controller file named "Upload.php" inside "application/controllers" folder.
Step 5: Create a view
Create a view file named "index.php" inside "application/views/upload" folder
Demo [sociallocker] Download[/sociallocker]
Step 1: Create MySQL Database and Table
//Table structure for table `picture`
CREATE TABLE `picture` (
`id` int(11) NOT NULL,
`url` int(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
// Indexes for table `picture`
ALTER TABLE `picture` ADD PRIMARY KEY (`id`);
//AUTO_INCREMENT for table `picture`
ALTER TABLE `picture` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
?>
Step 2: Create a model file
Create a model file named "Upload_model.php" inside "application/models" folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Upload Model: CodeIgniter Upload image with MySQL
*
* @author TechArise Team
*
* @email info@techarise.com
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Upload_model extends CI_Model {
private $_ID;
private $_url;
public function setID($ID) {
$this->_ID = $ID;
}
public function setURL($url) {
$this->_url = $url;
}
// get image
public function getPicture() {
$this->db->select(array('p.id', 'p.url'));
$this->db->from('picture p');
$this->db->where('p.id', $this->_ID);
$query = $this->db->get();
return $query->row_array();
}
// insert image
public function create() {
$data = array(
'url' => $this->_url,
);
$this->db->insert('picture', $data);
return $this->db->insert_id();
}
}
?>
Step 3: Update constants file
Add code the file application/config/constants.php in your CodeIgniter installation with you controller’s name.
$root = "http://" . $_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
$constant['base_url'] = $root;
define('HTTP_UPLOAD_LARGE_PATH', $constant['base_url'] . 'assets/uploads/_large/');
define('HTTP_UPLOAD_MEDIUM_PATH', $constant['base_url'] . 'assets/uploads/_medium/');
define('HTTP_UPLOAD_THUMB_PATH', $constant['base_url'] . 'assets/uploads/_thumb/');
define('HTTP_UPLOAD_MOBILE_PATH', $constant['base_url'] . 'assets/uploads/_mobile/');
define('ROOT_UPLOAD_PATH', BASH_PATH . 'assets/uploads/');
?>
Step 4: Create a controller file
Next create a controller file named "Upload.php" inside "application/controllers" folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* @package Upload : CodeIgniter Upload Image with MySQL
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Upload Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Upload_model', 'upl');
}
// upload image
public function index() {
$data['page'] = 'upload-img';
$data['title'] = 'Upoad Image | TechArise';
$this->load->view('upload/index', $data);
}
// action save method
public function save() {
if ($this->input->post('upload_img')) {
$path = ROOT_UPLOAD_PATH;
// Define file rules
$initi = $this->upload->initialize(array(
"upload_path" => $path,
"allowed_types" => "gif|jpg|jpeg|png|bmp",
"remove_spaces" => TRUE
));
$imagename = 'no-img.jpg';
if (!$this->upload->do_upload('imageURL')) {
$error = array('error' => $this->upload->display_errors());
echo $this->upload->display_errors();
} else {
$data = $this->upload->data();
$imagename = $data['file_name'];
}
// create Thumbnail -- IMAGE_SIZES;
$image_sizes = array('_mobile'=>array(75,75), '_thumb' => array(300, 200),'_medium' => array(500, 270), '_large' => array(750, 406));
// load library
$this->load->library('image_lib');
foreach ($image_sizes as $key=>$resize) {
$config = array(
'source_image' => $data['full_path'],
'new_image' => ROOT_UPLOAD_PATH .'/'.$key,
'maintain_ratio' => FALSE,
'width' => $resize[0],
'height' => $resize[1],
'quality' =>70,
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->upl->setURL($imagename);
$this->upl->create();
$this->session->set_flashdata('img_uploaded_msg', 'Image uploaded successfully!');
$this->session->set_flashdata('img_uploaded', $imagename);
redirect('/');
}
}
}
?>
Step 5: Create a view
Create a view file named "index.php" inside "application/views/upload" folder
$this->load->view('templates/header');
?>
Upload Image and Create Multiple Thumbnail Sizes using CodeIgniter with MySQL
session->flashdata('img_uploaded_msg'); ?>
session->flashdata('img_uploaded'))) { ?>
Images
$this->load->view('templates/footer');
?>
Demo [sociallocker] Download[/sociallocker]