oyazhuryachna

controller_upload_wo_db

Apr 27th, 2017
2,881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Upload extends CI_Controller {
  5.  
  6.  
  7.     public function index()
  8.     {
  9.         $list_file=array();
  10.         $dir = "uploaded_file/";
  11.  
  12.         // buka directory, dan baca isinya
  13.         if (is_dir($dir)){
  14.           if ($dh = opendir($dir)){
  15.             while (($file = readdir($dh)) !== false){
  16.                 $list_file[]=$file;
  17.             }
  18.             closedir($dh);
  19.           }
  20.         }
  21.  
  22.         $data['daftar_file']=$list_file;
  23.         $this->load->view('home',$data);
  24.     }
  25.    
  26.    
  27.     public function form(){
  28.         $data['page']='form_upload';
  29.         $this->load->view('home',$data);
  30.     }
  31.    
  32.     public function do_upload(){
  33.         $config['upload_path']   = './uploaded_file/';
  34.         $config['allowed_types'] = 'doc|docx|xls|xlsx|pdf|zip|rar';
  35.         $this->load->library('upload', $config);
  36.                
  37.                  if ( ! $this->upload->do_upload('file_nya')) {
  38.                     $data['error_upload'] = array('error' => $this->upload->display_errors());
  39.                     $this->session->set_userdata('status_upload',
  40.                     '<div class="alert alert-warning alert-dismissible" role="alert">
  41.                                                             <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'.
  42.                     $data['error_upload']['error'].'</div>');
  43.                  }
  44.                    
  45.                  else {
  46.                     $this->session->set_userdata('status_upload','<div class="alert alert-success alert-dismissible" role="alert">
  47.                                                             <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  48.                                                             File berhasil diupload
  49.                                                             </div>');
  50.                                                            
  51.                 }
  52.    
  53.             redirect(base_url());
  54.         }
  55.        
  56.     public function hapus($filenya){
  57.         $dir   = './uploaded_file/';
  58.         if(unlink($dir.$filenya)){
  59.         $this->session->set_userdata('status_hapus','<div class="alert alert-success alert-dismissible" role="alert">
  60.                                                             <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  61.                                                             File berhasil dihapus</div>');
  62.  
  63.         }  
  64.         redirect(base_url());
  65.     }  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment