Product: FILE uploads

This commit is contained in:
Thanakorn S
2026-04-21 11:31:51 +07:00
parent 3b8f94f011
commit 8834d672ba
7 changed files with 138 additions and 40 deletions
+13 -5
View File
@@ -326,6 +326,19 @@ function ajax_request(options) {
options.data = { json: JSON.stringify(payloadJson) };
if (options.debugMode) {
isAjaxProcessing = false;
// Show FormData contents if applicable
if (options.formData instanceof FormData) {
// Log original formData before merging
for (let [key, value] of options.formData.entries()) {
console.log("FORMDATA: " + key, value);
}
}
// Show stringified JSON payload
console.log("REQUEST DATA:", options.data);
}
// IF formData exist, we pass as $_POST [not json]
if (options.formData instanceof FormData) {
// THE BYPASS: If formData exists, move all text data into it
@@ -340,11 +353,6 @@ function ajax_request(options) {
// --- START MODIFIED $.AJAX BLOCK ---
let isSendingFiles = (options.data instanceof FormData);
if (options.debugMode) {
isAjaxProcessing = false;
console.log("REQUEST DATA:", options.data);
}
// Show loading overlay
if (options.noLoading !== true) {
$.LoadingOverlay("show", {
+29 -7
View File
@@ -289,10 +289,12 @@ class FileUploader {
private function ensureDirectory() {
if (!is_dir($this->target_dir)) {
mkdir($this->target_dir, 0755, true);
if (!mkdir($this->target_dir, 0755, true)) {
throw new Exception("Failed to create directory: " . $this->target_dir);
}
}
if (!is_writable($this->target_dir)) {
throw new Exception("Target directory is not writable.");
throw new Exception("Target directory is not writable: " . $this->target_dir);
}
}
@@ -325,16 +327,25 @@ class FileUploader {
$errors = [];
foreach ($_FILES[$field_name]['name'] as $key => $name) {
$error_code = $_FILES[$field_name]['error'][$key];
// Normalize single file to array structure
$files = $_FILES[$field_name];
if (!is_array($files['name'])) {
$files['name'] = [$files['name']];
$files['tmp_name'] = [$files['tmp_name']];
$files['error'] = [$files['error']];
$files['size'] = [$files['size']];
}
foreach ($files['name'] as $key => $name) {
$error_code = $files['error'][$key];
if ($error_code !== UPLOAD_ERR_OK) {
$errors[] = "{$name}: " . $this->getUploadError($error_code);
continue;
}
$tmp_name = $_FILES[$field_name]['tmp_name'][$key];
$file_size = $_FILES[$field_name]['size'][$key];
$tmp_name = $files['tmp_name'][$key];
$file_size = $files['size'][$key];
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if ($file_size > $this->max_size) {
@@ -356,7 +367,11 @@ class FileUploader {
continue;
}
$file_id = uniqid() . "_" . time() . "." . $extension;
// FILE NAME SANITIZATION + UNIQUE ID
$original = pathinfo($name, PATHINFO_FILENAME);
$original = preg_replace('/[^a-zA-Z0-9_-]/', '_', $original); // sanitize
$file_id = $original . "_" . uniqid() . "." . $extension;
$destination = $this->target_dir . $file_id;
if (move_uploaded_file($tmp_name, $destination)) {
@@ -378,6 +393,13 @@ class FileUploader {
$final = array_merge($keep, $this->uploaded_files);
return implode(",", array_filter($final));
}
/**
* Build final CSV string for DB as single file
*/
public function getUploadedFiles() {
return $this->uploaded_files;
}
/**
* Rollback uploaded files if DB fails