From ee37a4fa527db92276c5ca8236ca4a24d761b61c Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Mon, 4 May 2026 02:03:26 +0300 Subject: [PATCH] Update: 2026-05-04 02:03:26 --- app/bootstrap/init.php | 10 ++++++++++ app/modules_app/invoices/upload.php | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/bootstrap/init.php b/app/bootstrap/init.php index cc91c60..dbe0414 100644 --- a/app/bootstrap/init.php +++ b/app/bootstrap/init.php @@ -14,6 +14,16 @@ define('STORAGE_PATH', ROOT_PATH . '/storage'); require_once APP_PATH . '/bootstrap/env.php'; require_once APP_PATH . '/helpers/helpers.php'; +// Self-healing Storage +$dirs = ['/cache', '/logs', '/invoices', '/exports']; +foreach ($dirs as $d) { + $path = STORAGE_PATH . $d; + if (!is_dir($path)) { + mkdir($path, 0777, true); + chmod($path, 0777); + } +} + // 3. Error Reporting (Secure for production) if (env('APP_DEBUG', 'false') === 'true') { error_reporting(E_ALL); diff --git a/app/modules_app/invoices/upload.php b/app/modules_app/invoices/upload.php index 338236b..f1764c7 100644 --- a/app/modules_app/invoices/upload.php +++ b/app/modules_app/invoices/upload.php @@ -34,19 +34,19 @@ if (!$stmt->fetch()) { json_error('Access denied to this company or invalid company ID', 403); } -// 4. Handle File Upload +// 4. Handle File Upload (Step-by-step for permission safety) +$tenantDir = STORAGE_PATH . '/invoices/' . $tenantId; +$companyDir = $tenantDir . '/' . $companyId; $dateFolder = date('Y-m-d'); -$baseInvoicesDir = STORAGE_PATH . '/invoices'; -if (!is_dir($baseInvoicesDir)) { - mkdir($baseInvoicesDir, 0777, true); -} +$uploadDir = $companyDir . '/' . $dateFolder . '/'; -$uploadDir = $baseInvoicesDir . '/' . $tenantId . '/' . $companyId . '/' . $dateFolder . '/'; - -if (!is_dir($uploadDir)) { - if (!mkdir($uploadDir, 0777, true)) { - error_log("UPLOAD ERROR: Failed to create directory: " . $uploadDir); - json_error('فشل في إنشاء مجلد التخزين: ' . $uploadDir, 500); +foreach ([$tenantDir, $companyDir, $uploadDir] as $dir) { + if (!is_dir($dir)) { + if (!mkdir($dir, 0777, true)) { + error_log("UPLOAD ERROR: Failed to create directory: " . $dir); + json_error('فشل في إنشاء مجلد التخزين: ' . $dir, 500); + } + chmod($dir, 0777); // Force permissions } }