File: /home/cafsindia/login_cafsindia_com/application/controllers/File_uploading.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class File_uploading extends Action_controller{
public function __construct(){
parent::__construct('file_uploading');
}
// LOAD PAGE INPUT CONTENTS AND TABLE HEADERS
public function index(){
$this->load->view("file_uploading/manage");
}
// STORE THE CORRECT DOCUMENTS
public function upload_documents(){
$process_month = $this->input->post('process_month');
$folder_name = $this->input->post('file'); // Folder name, not the uploaded file
$file_path = "./emp_documents/$folder_name/$process_month/"; // Destination path
$temp_path = "./temp_documents/"; // Temporary path
$tr_line = '';
if(!file_exists($file_path)){
mkdir($file_path, 0777, true);
}
if(!file_exists($temp_path)){
mkdir($temp_path, 0777, true);
}
if(!isset($_FILES['file']) || !is_array($_FILES['file']['name'])){
echo json_encode(['success' => false, 'message' => 'No files uploaded.']);
return;
}
$login_code_qry = 'SELECT login_code FROM cw_employees WHERE employee_status = 1';
$login_code_data = $this->db->query("CALL sp_a_run ('SELECT','$login_code_qry')");
$login_result = $login_code_data->result_array(); // get result as array
$login_code_data->next_result();
$processed_count = 0;
$folder_err_count = 0;
$error_messages = [];
$status = [];
$total_files = count($_FILES['file']['name']);
for($i = 0; $i < $total_files; $i++){
$file_name = $_FILES['file']['name'][$i];
$file_tmp = $_FILES['file']['tmp_name'][$i];
$uploaded_file = $temp_path . basename($file_name);
$file_extension = strtolower(pathinfo($uploaded_file, PATHINFO_EXTENSION));
if(in_array($file_extension, ['zip', 'rar', 'pdf'])){
if(move_uploaded_file($file_tmp, $uploaded_file)){
// Extract if compressed
if($file_extension === 'zip'){
$zip = new ZipArchive();
if($zip->open($uploaded_file) === true){
$zip->extractTo($temp_path);
$zip->close();
unlink($uploaded_file);
}
}else if($file_extension === 'rar'){
$rar = RarArchive::open($uploaded_file);
if($rar !== false){
foreach ($rar->getEntries() as $entry) {
$entry->extract($temp_path);
}
$rar->close();
unlink($uploaded_file);
}
}
// Process extracted or directly uploaded files
$files = scandir($temp_path);
foreach($files as $inner_file){
if ($inner_file === '.' || $inner_file === '..') continue;
$old_path = $temp_path . $inner_file;
if(pathinfo($inner_file, PATHINFO_EXTENSION) === 'pdf'){
$new_name = $this->file_code_convert($inner_file, $old_path, $login_result);
if($new_name){
$new_path = $file_path . $new_name;
if(rename($old_path, $new_path)){
$status['success'][] = ['file_name' => $inner_file,'message' => 'Success'];
$processed_count++;
}else{
$status['failed'][] = ['file_name' => $inner_file,'message' => 'Failed'];
}
}else{
unlink($old_path);
$status['failed'][] = ['file_name' => $inner_file,'message' => 'Invalid login code'];
}
}else if(is_dir($old_path)){
// If path has a directory that will process again
$folder_files = scandir($old_path);
foreach($folder_files as $fol_in_file){
if($fol_in_file === '.' || $fol_in_file === '..') continue;
$new_temp_path = $old_path . '/' . $fol_in_file;
if(pathinfo($fol_in_file, PATHINFO_EXTENSION) === 'pdf'){
$new_name = $this->file_code_convert($fol_in_file, $new_temp_path, $login_result);
if($new_name){
$new_path = $file_path . $new_name;
if(rename($new_temp_path, $new_path)){
$status['success'][] = ['file_name' => $fol_in_file,'message' => 'Success'];
$processed_count++;
}else{
$status['failed'][] = ['file_name' => $fol_in_file,'message' => 'Failed'];
}
}else{
unlink($new_temp_path);
$status['failed'][] = ['file_name' => $fol_in_file,'message' => 'Invalid login code'];
}
}else{
if(is_dir($new_temp_path)){
$this->delete_folder($new_temp_path);
$status['failed'][] = ['file_name' => $fol_in_file,'message' => 'ZIP/RAR files containing another folders are not allowed; only PDF files are permitted.'];
$folder_err_count++;
}
}
}
}
}
}else{
$error_messages[] = "$file_name: Failed to upload.";
}
}else{
$error_messages[] = "$file_name: Only ZIP, RAR, and PDF files are allowed.";
}
}
$this->delete_folder($temp_path); // Clean up temp folder
// Generate table rows
foreach ($status as $key => $file_data) {
$color = ($key === 'success') ? '#9ADE7B' : '#EB5353';
foreach ($file_data as $data_message) {
$tr_line .= "<tr><td>{$data_message['file_name']}</td><td style='color:$color;'>{$data_message['message']}</td></tr>";
}
}
$table_data = "<table class='table' id='mytable'>
<thead>
<tr style='background-color:#001630;color:white'>
<th>File Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
$tr_line
</tbody>
</table>";
if(($processed_count === $total_files || $processed_count > $total_files) && empty($status['failed'])){
echo json_encode(['success' => true,'message' => 'All files uploaded and processed successfully.','table_data' => $table_data]);
}else if(($processed_count > 0 || $folder_err_count > 0)||count($status['failed']) > 0){
echo json_encode(['success' => true,'message' => 'Some files processed with issues.','table_data' => $table_data]);
}else{
echo json_encode(['success' => false,'message' => $error_messages]);
}
}
// FILE EMP CODE ENCODE AND CREATE NEW NAME
public function file_code_convert($filecode,$old_path,$login_code_array){
$file_extension = pathinfo($filecode, PATHINFO_EXTENSION);
$file_log_code = pathinfo($filecode, PATHINFO_FILENAME);
if (in_array($file_log_code, array_column($login_code_array, 'login_code'))){
$file_enc_code = $this->encrypt_file_name($file_log_code);
}else{
unlink($old_path);
return false;
}
$new_file_name = $file_log_code .'_'. $file_enc_code . '.' . $file_extension;
return preg_replace('/[^a-zA-Z0-9_\-\.]/', '', $new_file_name);
}
// DELETE THE UNZIP OR UNRAR FILES HAVE FOLDER THAT WILL DELETE
public function delete_folder($folder_path){
$files = scandir($folder_path);
foreach ($files as $file) {
if ($file === '.' || $file === '..') continue;
$file_path = $folder_path . DIRECTORY_SEPARATOR . $file;
if(is_dir($file_path)){
$this->delete_folder($file_path);
}else{
unlink($file_path);
}
}
rmdir($folder_path);
}
}
?>