Create Controller
application\controllers\upload.php
Create folder name uploads root directory
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->library('table');
$this->load->helper('form');
}
public function index() {
$this->load->view('upload_form');
}
public function upload_success() {
$this->load->view('test/upload_success');
}
public function doUpload() {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) echo $this->upload->display_errors();
else {
$fInfo = $this->upload->data();
$this->_createThumbnail($fInfo['file_name']);
$data['uploadInfo'] = $fInfo;
$data['thumbnail_name'] = $fInfo['raw_name'] . '_thumb' . $fInfo['file_ext'];
$this->load->view('test/upload_success', $data);
}
}
public function _createThumbnail($fileName) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/' . $fileName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 75;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}
}
Create View application\views\upload_form.php
<html>
<head>
<title>Upload an Image </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="container">
<h2>Upload an Image </h2>
<?php echo form_open_multipart('upload/doUpload'); ?>
<input type="file" name="userfile" />
<p><input type="submit" value="Submit" name="submit" /></p>
<?php echo form_close(); ?>
</div>
</body>
</html>
Create View application\views\test\upload_success.php
