$item, 'path' => $full_path, 'size' => is_file($full_path) ? filesize($full_path) : 0, 'type' => is_dir($full_path) ? 'directory' : 'file', 'modified' => date('Y-m-d H:i:s', filemtime($full_path)) ]; } } return $files; } function format_size($bytes) { if ($bytes >= 1073741824) { return number_format($bytes / 1073741824, 2) . ' GB'; } elseif ($bytes >= 1048576) { return number_format($bytes / 1048576, 2) . ' MB'; } elseif ($bytes >= 1024) { return number_format($bytes / 1024, 2) . ' KB'; } else { return $bytes . ' bytes'; } } // ==================== REQUESTED DIRECTORY HANDLING ==================== $requested_dir = isset($_GET['dir']) ? sanitize_path($_GET['dir']) : basename($base_dir); $current_dir_candidate = $root_anchor . ($requested_dir ? DIRECTORY_SEPARATOR . $requested_dir : ''); $current_dir = safe_realpath_within($current_dir_candidate, $root_anchor); if($current_dir === false){ $requested_dir = basename($base_dir); $current_dir = safe_realpath_within($root_anchor . DIRECTORY_SEPARATOR . $requested_dir, $root_anchor); if($current_dir === false){ $requested_dir = ''; $current_dir = safe_realpath_within($root_anchor, $root_anchor); } } // ==================== PARENT DIRECTORY CALCULATION ==================== if($requested_dir !== ''){ $parent_dir = dirname($requested_dir); if($parent_dir === '.' || $parent_dir === '\\') $parent_dir = ''; } else { $parent_dir = ''; } // ==================== BREADCRUMB ==================== $dir_parts = []; if($requested_dir !== ''){ $parts = explode('/', $requested_dir); $acc = ''; foreach($parts as $p){ if($p === '') continue; $acc .= ($acc === '' ? '' : '/') . $p; $dir_parts[] = ['name' => $p, 'path' => $acc]; } } // ==================== ACTION HANDLING ==================== $action = $_GET['action'] ?? 'list'; switch($action){ case 'view': $file = sanitize_path($_GET['file'] ?? ''); $file_path = $current_dir . DIRECTORY_SEPARATOR . $file; if(safe_realpath_within($file_path, $root_anchor) && is_file($file_path)){ header('Content-Type: text/plain'); readfile($file_path); exit; } break; case 'edit': $file = sanitize_path($_GET['file'] ?? ''); $file_path = $current_dir . DIRECTORY_SEPARATOR . $file; if($_SERVER['REQUEST_METHOD'] === 'POST'){ $content = $_POST['content'] ?? ''; if(safe_realpath_within($file_path, $root_anchor) && is_file($file_path)){ file_put_contents($file_path, $content); } header('Location: ?action=list&dir=' . urlencode($requested_dir)); exit; } if(safe_realpath_within($file_path, $root_anchor) && is_file($file_path)){ $content = htmlspecialchars(file_get_contents($file_path)); echo "
Upload files or create new folders to get started
| Name | Type | Size | Modified | Actions |
|---|---|---|---|---|
| = htmlspecialchars($f['name']) ?> | = $f['type'] ?> | = $f['type'] === 'directory' ? '-' : format_size($f['size']) ?> | = $f['modified'] ?> |