From e5e970386e8a5a60c361419ae743a9f9fd7ad493 Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Tue, 3 Feb 2026 13:05:34 +0530 Subject: [PATCH] FIX_ISSUE --- app/Helpers/utility_helper.php | 53 +++++++++++++++++----------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/app/Helpers/utility_helper.php b/app/Helpers/utility_helper.php index b23a8338..3d153d75 100755 --- a/app/Helpers/utility_helper.php +++ b/app/Helpers/utility_helper.php @@ -947,37 +947,38 @@ if (!function_exists('validateExcelFile')) { function validateExcelFile($file) { - $allowed = [ - // Standard Excel Formats - 'application/vnd.ms-excel', // .xls - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // .xlsx - 'application/vnd.oasis.opendocument.spreadsheet', // .ods (OpenDocument) + // 1. Check for upload errors first + if ($file->getError() !== UPLOAD_ERR_OK) { + return false; + } - // Macro-Enabled & Specialized Formats - 'application/vnd.ms-excel.sheet.macroEnabled.12', // .xlsm - 'application/vnd.ms-excel.template.macroEnabled.12', // .xltm - 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', // .xlsb - 'application/vnd.ms-excel.addin.macroEnabled.12', // .xlam + // 2. Size check (16MB) + if ($file->getSize() > (16 * 1024 * 1024)) { + return false; + } - // Common "Edge Case" and Legacy Types - 'application/excel', // Legacy - 'application/x-excel', // Legacy/Vendor - 'application/x-msexcel', // Legacy/Vendor - 'application/x-ms-excel', // Legacy/Vendor - 'application/x-dos_ms_excel', // Very old legacy - 'text/xls', // Common mislabel - 'text/xlsx' // Common mislabel + $allowedMimeTypes = [ + 'application/vnd.ms-excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/vnd.oasis.opendocument.spreadsheet', + 'application/vnd.ms-excel.sheet.macroEnabled.12', + 'application/zip', // Crucial: Many servers see .xlsx as a zip + 'application/octet-stream' // Fallback for some browser transfers ]; - // if ($file->getError() !== UPLOAD_ERR_OK) return 'Upload error'; - // if ($file->getSize() > (16 * 1024 * 1024)) return 'File too large'; - // if (!in_array($file->getClientMimeType(), $allowed, true)) return 'Invalid file type'; - - if ($file->getError() !== UPLOAD_ERR_OK) return false; - if ($file->getSize() > (16 * 1024 * 1024)) return false; - if (!in_array($file->getClientMimeType(), $allowed, true)) return false; + $allowedExtensions = ['xls', 'xlsx', 'xlsm', 'xlsb', 'ods']; - return true; + // Get file info + $mime = $file->getClientMimeType(); + $extension = strtolower($file->getClientOriginalExtension()); + + // 3. Dual Validation: Check if EITHER the mime is in our list OR the extension is valid + // This solves issues where the server/browser misidentifies the MIME type. + if (in_array($mime, $allowedMimeTypes, true) || in_array($extension, $allowedExtensions, true)) { + return true; + } + + return false; } }