From 78bc67fc8a9ebf8230600e37fe9e15fecd57c56d Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Tue, 14 Apr 2026 16:22:50 +0700 Subject: [PATCH] fix: robust activity attachment path resolution --- app/Views/activities_detail.php | 49 +++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/app/Views/activities_detail.php b/app/Views/activities_detail.php index 1397abb..a9552e5 100644 --- a/app/Views/activities_detail.php +++ b/app/Views/activities_detail.php @@ -7,18 +7,49 @@ $filelist = $attachment; $file_array = explode (',', $filelist); function resolve_attachment_relative_path($filename) { - $uploadRoot = FCPATH . 'file' . DIRECTORY_SEPARATOR; - $pattern = $uploadRoot . '*' . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . $filename; - $matches = @glob($pattern); - if ($matches !== false) { - foreach ($matches as $match) { - if (is_file($match)) { - $relative = str_replace('\\', '/', substr($match, strlen($uploadRoot))); - return 'file/' . $relative; + $filename = trim($filename); + if ($filename === '') { + return ''; + } + + $normalized = str_replace('\\', '/', ltrim($filename, '/')); + + $directCandidates = []; + if (strpos($normalized, 'file/') === 0 || strpos($normalized, 'upload/') === 0) { + $directCandidates[] = $normalized; + } else { + $directCandidates[] = 'upload/' . $normalized; + $directCandidates[] = 'file/' . $normalized; + } + + foreach ($directCandidates as $candidate) { + $absolutePath = FCPATH . str_replace('/', DIRECTORY_SEPARATOR, $candidate); + if (is_file($absolutePath)) { + return $candidate; + } + } + + $basename = basename($normalized); + if ($basename === '') { + return 'upload/' . $normalized; + } + + $roots = ['file', 'upload']; + foreach ($roots as $root) { + $rootPath = FCPATH . $root . DIRECTORY_SEPARATOR; + $pattern = $rootPath . '*' . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . $basename; + $matches = @glob($pattern); + if ($matches !== false) { + foreach ($matches as $match) { + if (is_file($match)) { + $relative = str_replace('\\', '/', substr($match, strlen($rootPath))); + return $root . '/' . $relative; + } } } } - return 'file/' . $filename; + + return 'upload/' . $basename; } $i = 1;