search bar?
this is wonderfull sample for learning but there is no search. can be add a search bar?
localhost/codeigniter_fileupload
$config['base_url'] = 'http://localhost/codeigniter_fileupload/';
<?php class Files_model extends CI_Model { function __construct(){ parent::__construct(); $this->load->database(); } public function getAllFiles(){ $query = $this->db->get('files'); return $query->result(); } public function insertfile($file){ return $this->db->insert('files', $file); } } ?>
<?php class Upload extends CI_Controller { function __construct() { parent::__construct(); //load our helper $this->load->helper('url'); //load our model $this->load->model('files_model'); } public function index(){ //load session library to use flashdata $this->load->library('session'); //fetch all files i the database $data['files'] = $this->files_model->getAllFiles(); $this->load->view('file_upload', $data); } public function insert(){ //load session library to use flashdata $this->load->library('session'); //Check if file is not empty $config['upload_path'] = 'upload/'; //restrict uploads to this mime types $config['allowed_types'] = 'jpg|jpeg|png|gif'; $config['file_name'] = $_FILES['upload']['name']; //Load upload library and initialize configuration $this->load->library('upload', $config); $this->upload->initialize($config); if($this->upload->do_upload('upload')){ $uploadData = $this->upload->data(); $filename = $uploadData['file_name']; //set file data to insert to database $file['description'] = $this->input->post('description'); $file['filename'] = $filename; $query = $this->files_model->insertfile($file); if($query){ $this->session->set_flashdata('success','File uploaded successfully'); } else{ $this->session->set_flashdata('error','File uploaded but not inserted to database'); } }else{ $this->session->set_flashdata('error','Cannot upload file.'); } }else{ $this->session->set_flashdata('error','Cannot upload empty file.'); } } }
$route['default_controller'] = 'upload';
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CodeIgniter File Upload</title> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1 class="page-header text-center">CodeIgniter File Upload with Flashdata</h1> <div class="row"> <div class="col-sm-4"> <h3>File Upload Form</h3> <form method="POST" action="<?php echo base_url(); ?>index.php/upload/insert" enctype="multipart/form-data"> <div class="form-group"> <label>Description:</label> <input type="text" name="description" class="form-control" required> </div> <div class="form-group"> <label>File:</label> <input type="file" name="upload" required> </div> <button type="submit" class="btn btn-primary">Save</button> </form> <?php if($this->session->flashdata('success')){ ?> <div class="alert alert-success text-center" style="margin-top:20px;"> <?php echo $this->session->flashdata('success'); ?> </div> <?php } if($this->session->flashdata('error')){ ?> <div class="alert alert-danger text-center" style="margin-top:20px;"> <?php echo $this->session->flashdata('error'); ?> </div> <?php } ?> </div> <div class="col-sm-8"> <table class="table table-bordered table-striped"> <thead> <tr> <th>ID</th> <th>Filename</th> <th>Description</th> </tr> </thead> <tbody> <?php foreach($files as $file){ ?> <tr> <td><?php echo $file->id; ?></td> <td><?php echo $file->filename; ?></td> <td><?php echo $file->description; ?></td> </tr> <?php } ?> </tbody> </table> </div> </div> </div> </body> </html>