Menu

Upload Image using CodeIgniter with MySQL

Upload Image 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. CodeIgniter’s File Uploading Class permits files to be uploaded. You can set various preferences, restricting the type and size of the files. Follow the steps shown in the given example to understand the file uploading process in CodeIgniter.

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_PATH', $constant['base_url'] . 'assets/uploads/');
define('ROOT_UPLOAD_PATH', BASH_PATH . 'assets/uploads/');
?>


Step 4: Create a controller file
Next create a controller file named "Image.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 Image : CodeIgniter Upload Image with MySQL
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Upload Image Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Image extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->model('Upload_model', 'upl');
}
// upload image
public function index() {
$data['page'] = 'image-img';
$data['title'] = 'Upload Image | TechArise';
$this->load->view('img/index', $data);
}
// action save method
public function save() {
$path = ROOT_UPLOAD_PATH;
// Define file rules
$initialize = $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'];
$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/img" folder

$this->load->view('templates/header');
?>


Upload Image using CodeIgniter with MySQL







session->flashdata('img_uploaded_msg'); ?>


session->flashdata('img_uploaded'))) { ?>



















Images





$this->load->view('templates/footer');
?>


Demo  [sociallocker] Download[/sociallocker]

Ads middle content1

Ads middle content2