Created
March 14, 2019 02:32
-
-
Save mdestafadilah/837d63f31ef729ad6493309266aec1e6 to your computer and use it in GitHub Desktop.
CodeIgniter Function Upload DropZone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addFile($act=null,$randomKey=null) | |
{ | |
$this->load->model('ManajemenSuratFileModel'); | |
if ($act == 'removeFile') { | |
$token=$this->input->post('token'); //exit(var_dump($token)); | |
$randomKey=$this->input->post('randomKey'); | |
$data = $this->db->get_where('manajemen_surat_file',array('token'=>$token,'random_key'=>$randomKey)); //exit(dump(show_last_query())); | |
if($data->num_rows()>0){ | |
$hasil=$data->row(); | |
$nama_file=$hasil->nama_file; | |
$data_real = ROOTPATH.'/supports/office/'.$nama_file; | |
// Add to array list | |
$the_files_is_here = array($data_real); | |
// Multiple File's | |
foreach ($the_files_is_here as $file) { | |
unlink($file); | |
} | |
// Single File | |
// if(file_exists($file=ROOTPATH.'/foto/real/'.$nama_foto)){ | |
// unlink($file); | |
// } | |
$this->db->delete('manajemen_surat_file',array('token'=>$token,'random_key'=>$randomKey)); | |
} | |
}elseif ($act == 'formFile') { | |
$data['randomKey'] = $randomKey; | |
$this->load->view('Office/formFile',$data); | |
} | |
elseif ($act == 'formListFile' && isset($randomKey)) { | |
$this->load->helper("file"); | |
$this->set('randomKey',$randomKey); | |
# source: https://www.startutorial.com/articles/view/dropzonejs-php-how-to-display-existing-files-on-server | |
$storeFolder = ROOTPATH.'/supports/office/'; | |
$ds = DIRECTORY_SEPARATOR; | |
$datas = $this->db->get_where('manajemen_surat_file',array('random_key'=>$randomKey))->result_array(); //exit(dump(show_last_query())); | |
$result = array(); | |
$files = scandir($storeFolder); //exit(dump($files)); | |
# Show's only File You Have! | |
if ( false!==$files ) { | |
foreach ( $datas as $data ) { // Ambil data berdasarkan ID Dokter | |
if ( '.'!=$files && '..'!=$files) { // Abaikan Folder | |
$obj['name'] = $data['nama_file']; | |
$obj['size'] = filesize($storeFolder.$ds.$data['nama_file']); | |
$obj['token']= $data['token']; | |
$obj['randomKey'] = $randomKey; | |
$result[] = $obj; | |
} | |
} | |
} | |
header("Content-type: text/json"); | |
header("Content-type: application/json"); | |
echo json_encode($result); | |
} | |
elseif ($act == 'addFile') { | |
$this->load->model('ManajemenSuratFileModel'); | |
$this->load->helper(array('layout','debug')); | |
$config['upload_path'] = ROOTPATH.'/supports/office'; | |
$config['allowed_types'] = 'pdf|doc|docx|xls|xlsx'; | |
$config['encrypt_name'] = true; | |
$this->load->library('upload',$config); | |
if($this->upload->do_upload('userfile')){ | |
$token=$this->input->post('token'); | |
$randomKey=$this->input->post('randomKey'); | |
$data=$this->upload->data(); //exit(dump($data)); | |
$source = ROOTPATH.'/supports/office'.$data['file_name']; | |
$random = $data['file_name']; | |
$file = $data['orig_name']; | |
$data_file = array( | |
'random_key' => $randomKey, | |
'nama_file' => $file, | |
'random_name' => $random, | |
'token' => $token, | |
'user_create' => $this->auth->get_user_id(), | |
); | |
//exit(var_dump($data_foto)); | |
$this->ManajemenSuratFileModel->save($data_file); //exit(var_dump(show_last_query())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment