= 1024 && $i < count($units) - 1) { $bytes /= 1024; $i++; } return round($bytes, 2) . ' ' . $units[$i]; } function formatDateTime(string $datetime): string { try { $ts = strtotime($datetime); if ($ts === false) return $datetime; return date('d.m.Y H:i', $ts); } catch (\Exception $e) { return $datetime; } } function encodePath(string $path): string { return rawurlencode($path); } function getRelativePath(string $fullPath): string { $entry = ENTRY_POINT; if ($fullPath === $entry) return '/'; if (strpos($fullPath, $entry . '/') === 0) { $rel = substr($fullPath, strlen($entry) + 1); return $rel === '' ? '/' : '/' . $rel; } return $fullPath; } function getBreadcrumbs(string $path): array { $entry = ENTRY_POINT; $parts = []; if ($path === $entry) { return [['label' => '/', 'path' => $entry]]; } // Root segment $parts[] = ['label' => '/', 'path' => $entry]; $rel = substr($path, strlen($entry) + 1); $segments = explode('/', $rel); $current = $entry; foreach ($segments as $segment) { if ($segment === '') continue; $current .= '/' . $segment; $parts[] = ['label' => $segment, 'path' => $current]; } return $parts; } // ── API: Directory Listing ───────────────────────────────────────────────── function handleList(): void { $rawPath = $_GET['path'] ?? ''; $currentPath = $rawPath === '' ? ENTRY_POINT : ENTRY_POINT . '/' . urldecode($rawPath); // Resolve to absolute path inside ENTRY_POINT $currentPath = realpath($currentPath); if ($currentPath === false || strpos($currentPath, ENTRY_POINT) !== 0) { http_response_code(404); echo json_encode(['error' => 'Directory not found']); return; } $items = []; if ($dh = opendir($currentPath)) { while (($entry = readdir($dh)) !== false) { if ($entry === '.' || $entry === '..') continue; $fullPath = $currentPath . '/' . $entry; $stat = stat($fullPath); $items[] = [ 'name' => $entry, 'type' => is_dir($fullPath) ? 'dir' : 'file', 'size' => is_dir($fullPath) ? null : ($stat !== false ? $stat['size'] : 0), 'modified' => $stat !== false ? date('Y-m-d H:i:s', $stat['mtime']) : '', 'path' => $fullPath, ]; } closedir($dh); } echo json_encode($items); } // ── API: File Download ───────────────────────────────────────────────────── function handleDownload(): void { $rawFile = $_GET['file'] ?? ''; $filePath = ENTRY_POINT . '/' . urldecode($rawFile); if (!is_file($filePath)) { http_response_code(404); echo 'File not found'; return; } header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filePath) . '"'); header('Content-Length: ' . filesize($filePath)); header('Cache-Control: no-cache'); header('Pragma: no-cache'); readfile($filePath); exit; } // ── API Router ───────────────────────────────────────────────────────────── if (isset($_GET['action'])) { header('Content-Type: application/json'); if ($_GET['action'] === 'list') { handleList(); exit; } if ($_GET['action'] === 'download') { header('Content-Type: application/octet-stream'); handleDownload(); exit; } } // ── HTML Output ──────────────────────────────────────────────────────────── ?> File Browser

Lade Dateien…