From 3976a5346b81074e37afa678a4618c2f92601cba Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Mon, 4 May 2026 01:57:45 +0300 Subject: [PATCH] Update: 2026-05-04 01:57:45 --- app/modules_app/invoices/upload.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/modules_app/invoices/upload.php b/app/modules_app/invoices/upload.php index c358061..d984b62 100644 --- a/app/modules_app/invoices/upload.php +++ b/app/modules_app/invoices/upload.php @@ -37,17 +37,27 @@ if (!$stmt->fetch()) { // 4. Handle File Upload $dateFolder = date('Y-m-d'); $uploadDir = STORAGE_PATH . '/invoices/' . $tenantId . '/' . $companyId . '/' . $dateFolder . '/'; -if (!is_dir($uploadDir)) mkdir($uploadDir, 0775, true); + +if (!is_dir($uploadDir)) { + if (!mkdir($uploadDir, 0777, true)) { + error_log("UPLOAD ERROR: Failed to create directory: " . $uploadDir); + json_error('فشل في إنشاء مجلد التخزين', 500); + } +} $extension = pathinfo($_FILES['invoice']['name'], PATHINFO_EXTENSION); $fileName = bin2hex(random_bytes(8)) . '_' . time() . '.' . $extension; $targetFile = $uploadDir . $fileName; if (move_uploaded_file($_FILES['invoice']['tmp_name'], $targetFile)) { - // 5. Run AI Extraction $mimeType = $_FILES['invoice']['type']; - $base64Data = base64_encode(file_get_contents($targetFile)); + $fileContent = file_get_contents($targetFile); + if (!$fileContent) { + error_log("UPLOAD ERROR: Failed to read file content: " . $targetFile); + json_error('فشل في قراءة الملف المرفوع', 500); + } + $base64Data = base64_encode($fileContent); $extracted = \App\Core\AI::extractInvoiceData($base64Data, $mimeType); @@ -122,5 +132,7 @@ if (move_uploaded_file($_FILES['invoice']['tmp_name'], $targetFile)) { json_error('حدث خطأ أثناء حفظ بيانات الفاتورة', 500); } } else { - json_error('Failed to save uploaded file', 500); + $uploadError = $_FILES['invoice']['error'] ?? 'Unknown'; + error_log("UPLOAD ERROR: move_uploaded_file failed. Error Code: $uploadError. Target: $targetFile. Tmp: " . ($_FILES['invoice']['tmp_name'] ?? 'N/A')); + json_error('Failed to save uploaded file. PHP Error Code: ' . $uploadError, 500); }