'', 'path' => null, 'exists' => false, 'size' => 0, 'mime' => 'application/octet-stream', 'is_pdf' => false, 'is_image' => false, ]; } $cod = str_pad((string) $codStabile, 4, '0', STR_PAD_LEFT); $candidateSubdirs = [ $preferredSubdir, 'E_C', 'e_c', 'INC_EC', 'inc_ec', 'allegati', 'Allegati', 'WA', 'wa', 'XML', 'xml', 'posta_ordinaria', 'posta_pec', 'posta', 'documenti', 'Documenti', '', ]; $candidateSubdirs = array_values(array_unique(array_filter($candidateSubdirs, fn($v) => $v !== null))); $baseRoots = [ "/mnt/gescon-archives/gescon/{$cod}", "/mnt/gescon-archives/gescon/" . ltrim((string) $codStabile, '0'), storage_path("app/private/amministratori/ADMX3PD9/stabili/{$cod}"), storage_path("app/private/amministratori/ADMX3PD9/stabili/" . ltrim((string) $codStabile, '0')), storage_path("app/public/stabili/{$cod}"), storage_path("app/public/stabili/" . ltrim((string) $codStabile, '0')), ]; $foundPath = null; // 1. Cerca percorsi esatti foreach ($baseRoots as $root) { if (! is_dir($root)) { continue; } foreach ($candidateSubdirs as $sub) { $dir = $sub !== '' ? "{$root}/{$sub}" : $root; $p = "{$dir}/{$filename}"; if (file_exists($p)) { $foundPath = $p; break 2; } } } // 2. Se non trovato e il file ha un'estensione, cerca case-insensitive if (! $foundPath) { $lowerName = strtolower($filename); foreach ($baseRoots as $root) { if (! is_dir($root)) { continue; } foreach ($candidateSubdirs as $sub) { $dir = $sub !== '' ? "{$root}/{$sub}" : $root; if (is_dir($dir)) { $files = @scandir($dir); if (is_array($files)) { foreach ($files as $f) { if (strtolower($f) === $lowerName) { $foundPath = "{$dir}/{$f}"; break 3; } } } } } } } $exists = $foundPath !== null && file_exists($foundPath); $size = $exists ? (int) @filesize($foundPath) : 0; $mime = 'application/octet-stream'; $isPdf = str_ends_with(strtolower($filename), '.pdf'); $isImg = (bool) preg_match('/\.(jpg|jpeg|png|webp|gif|bmp)$/i', $filename); if ($exists) { $finfo = @finfo_open(FILEINFO_MIME_TYPE); if ($finfo) { $detected = @finfo_file($finfo, $foundPath); if ($detected) { $mime = $detected; } @finfo_close($finfo); } } if ($isPdf) { $mime = 'application/pdf'; } return [ 'filename' => $filename, 'path' => $foundPath, 'exists' => $exists, 'size' => $size, 'mime' => $mime, 'is_pdf' => $isPdf, 'is_image' => $isImg, ]; } }