fix: robust activity attachment path resolution

This commit is contained in:
mahdahar 2026-04-14 16:22:50 +07:00
parent 96493ac1a2
commit 78bc67fc8a

View File

@ -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;