CREATE TABLE
CREATE TABLE `files` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL COMMENT 'Upload Date',
`modified` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Unblock, 0=Block',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CONTROLLER
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_Files extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->model('file');
}
function index(){
$data = array();
if($this->input->post('fileSubmit') && !empty($_FILES['userFiles']['name'])){
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$uploadPath = 'uploads/files/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
}
if(!empty($uploadData)){
//Insert file information into the database
$insert = $this->file->insert($uploadData);
$statusMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.';
$this->session->set_flashdata('statusMsg',$statusMsg);
}
}
//Get files data from database
$data['files'] = $this->file->getRows();
//Pass the files data to view
$this->load->view('upload_files/index', $data);
}
}
MODEL
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class File extends CI_Model{
public function getRows($id = ''){
$this->db->select('id,file_name,created');
$this->db->from('files');
if($id){
$this->db->where('id',$id);
$query = $this->db->get();
$result = $query->row_array();
}else{
$this->db->order_by('created','desc');
$query = $this->db->get();
$result = $query->result_array();
}
return !empty($result)?$result:false;
}
public function insert($data = array()){
$insert = $this->db->insert_batch('files',$data);
return $insert?true:false;
}
}
VIEW
<div class="container">
<div class="row">
<p><?php echo $this->session->flashdata('statusMsg'); ?></p>
<form enctype="multipart/form-data" action="" method="post">
<div class="form-group">
<label>Choose Files</label>
<input type="file" class="form-control" name="userFiles[]" multiple/>
</div>
<div class="form-group">
<input class="form-control" type="submit" name="fileSubmit" value="UPLOAD"/>
</div>
</form>
</div>
<div class="row">
<ul class="gallery">
<?php if(!empty($files)): foreach($files as $file): ?>
<li class="item">
<img src="<?php echo base_url('uploads/files/'.$file['file_name']); ?>" alt="" >
<p>Uploaded On <?php echo date("j M Y",strtotime($file['created'])); ?></p>
</li>
<?php endforeach; else: ?>
<p>Image(s) not found.....</p>
<?php endif; ?>
</ul>
</div>
</div>
CONFIG
$autoload['libraries'] = array('session','database');
$autoload['helper'] = array('url');