File manager - Edit - /home/autoph/public_html/ltms-upload/upload.php
Back
<?php include_once('conn-db.php'); // Function to compress image function compressImage($source, $destination, $quality) { // Get image info $imgInfo = getimagesize($source); $mime = $imgInfo['mime']; // Create a new image from file switch($mime) { case 'image/jpeg': $image = imagecreatefromjpeg($source); break; case 'image/png': $image = imagecreatefrompng($source); break; case 'image/gif': $image = imagecreatefromgif($source); break; default: $image = imagecreatefromjpeg($source); } // Save image imagejpeg($image, $destination, $quality); imagedestroy($image); // Free up memory } // Handle file deletion if (isset($_REQUEST["request"]) && $_REQUEST["request"] === 'delete') { $file_name = $_REQUEST["name"]; $location = "../connectv1/dist/img/customer/ltms-customer-upload/" . $filename; if (file_exists($location)) { unlink($location); } exit; } // Handle file upload $return_arr = array(); $batch_id = generateUniqueBatchId(); // Using a function to generate the unique 6-character batch ID if (!empty($_FILES)) { $base_dir = "uploads"; // Base directory for uploads $sub_dir = $base_dir . "/" . $batch_id; // Subfolder named after the batch ID // Create the batch_id subfolder if it doesn't exist if (!is_dir($sub_dir)) { mkdir($sub_dir, 0777, true); // Recursively create the directory } foreach ($_FILES['files']['name'] as $index => $filename) { $file_tmp_name = $_FILES['files']['tmp_name'][$index]; $file_size = $_FILES['files']['size'][$index]; $file_error = $_FILES['files']['error'][$index]; if ($file_error === UPLOAD_ERR_OK) { $maxsize = 9097152; $minsize = 300000; if ($file_size >= $maxsize || $file_size == 0) { $return_arr["status"] = 0; $return_arr["message"] = "Image file too large. Image must be less than " . round(($maxsize / 1000000), 0) . " megabytes."; echo json_encode($return_arr); return; } $quality = $file_size >= $minsize ? ($file_size >= $maxsize / 2 ? 30 : 60) : 60; $valid_ext = array('png', 'jpeg', 'jpg'); $file_extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if (in_array($file_extension, $valid_ext)) { $location = "../connectv1/dist/img/customer/ltms-customer-upload/" . $filename; // $location = $sub_dir . "/" . $filename; // File path in the subfolder // $location = "uploads/" . $filename; //local dev // compressImage($file_tmp_name, $location, $quality); move_uploaded_file($file_tmp_name, $location); try { $pdo->beginTransaction(); $fullname = $_REQUEST['fullname']; $cs_number = $_REQUEST['cs_number']; $created_at = date('Y-m-d h:i:s'); $stmt = $pdo->prepare("INSERT INTO `customer_upload_ltms` (`reference`, `fullname`, `cs_number`, `filename`, `directory`, `extension`, `size`, `created_at`) VALUES (:reference, :fullname, :cs_number, :filename, :directory, :extension, :size, :created_at)"); $stmt->bindParam(':reference', $batch_id); $stmt->bindParam(':fullname', $fullname); $stmt->bindParam(':cs_number', $cs_number); $stmt->bindParam(':filename', $filename); $stmt->bindParam(':directory', $location); // $stmt->bindValue(':directory', 'uploads/'); //local $stmt->bindParam(':extension', $file_extension); $stmt->bindParam(':size', $file_size); $stmt->bindParam(':created_at', $created_at); $stmt->execute(); $pdo->commit(); $return_arr["status"] = 1; $return_arr["message"] = "Image uploaded and data inserted successfully."; $return_arr['reference'] = $batch_id; } catch (Exception $e) { // Rollback the transaction if something failed $pdo->rollBack(); error_log("Exception: " . $e->getMessage()); $return_arr["status"] = 0; $return_arr["message"] = "Error: " . $e->getMessage(); } } else { $return_arr["status"] = 0; $return_arr["message"] = "Invalid file type."; echo json_encode($return_arr); return; } } else { $return_arr["status"] = 0; $return_arr["message"] = "Error with file upload."; echo json_encode($return_arr); return; } } echo json_encode($return_arr); } else { $return_arr["status"] = 0; $return_arr["message"] = "No files uploaded."; echo json_encode($return_arr); } // Function to generate a unique batch ID (6 characters: combination of numbers and letters) function generateUniqueBatchId() { global $pdo; // Access the PDO object $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; $batch_id = ''; do { // Generate a random 6-character string $batch_id = ''; for ($i = 0; $i < 6; $i++) { $batch_id .= $characters[rand(0, strlen($characters) - 1)]; } // Check if the generated ID already exists in the database $stmt = $pdo->prepare("SELECT COUNT(*) FROM `customer_upload_ltms` WHERE `reference` = :reference"); $stmt->bindParam(':reference', $batch_id); $stmt->execute(); $count = $stmt->fetchColumn(); } while ($count > 0); // Keep generating if the ID already exists return $batch_id; } ?>
| ver. 1.4 |
.
| PHP 7.3.33 | Generation time: 0 |
proxy
|
phpinfo
|
Settings