Update: 2026-05-04 01:57:45
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user