Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Simple PHP File Manager – “Non Evil Code”
- // Lists directories/files, lets you edit file contents, change permissions, and navigate around.
- // WARNING: This demo does NOT include authentication. Do not use it in production without proper security measures.
- define('BASE_DIR', realpath(__DIR__ . '/files')); // Base directory for file operations
- // Utility function to sanitize and restrict navigation within BASE_DIR
- function sanitizePath($path) {
- $realBase = BASE_DIR;
- $realUserPath = realpath($path);
- if ($realUserPath === false || strpos($realUserPath, $realBase) !== 0) {
- return false;
- }
- return $realUserPath;
- }
- // Determine current directory from the GET parameter (default to BASE_DIR)
- $dirParam = isset($_GET['dir']) ? $_GET['dir'] : '';
- $currentDir = sanitizePath(BASE_DIR . '/' . $dirParam);
- if ($currentDir === false) {
- die("Invalid directory!");
- }
- // Display navigation breadcrumbs
- function displayBreadcrumbs($currentDir) {
- $relativePath = ltrim(str_replace(BASE_DIR, '', $currentDir), '/\\');
- $parts = $relativePath ? explode(DIRECTORY_SEPARATOR, $relativePath) : [];
- echo '<a href="?">BASE</a>';
- $pathSoFar = '';
- foreach ($parts as $part) {
- $pathSoFar .= '/' . $part;
- echo ' / <a href="?dir=' . urlencode(ltrim($pathSoFar, '/')) . '">' . htmlspecialchars($part) . '</a>';
- }
- echo "<br><br>";
- }
- // File editing functionality
- if (isset($_GET['action']) && $_GET['action'] === 'edit' && isset($_GET['file'])) {
- $filePath = sanitizePath($currentDir . '/' . $_GET['file']);
- if ($filePath === false || !is_file($filePath)) {
- die("Invalid file!");
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- // Save file content
- $newContent = $_POST['content'];
- file_put_contents($filePath, $newContent);
- echo "File updated successfully!<br>";
- echo '<a href="?dir=' . urlencode(ltrim(str_replace(BASE_DIR, '', $currentDir), '/')) . '">Back to list</a>';
- exit;
- }
- // Display file editing form
- $content = file_get_contents($filePath);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Edit File</title>
- </head>
- <body>
- <?php displayBreadcrumbs($currentDir); ?>
- <h2>Editing File: <?php echo htmlspecialchars($_GET['file']); ?></h2>
- <form method="post">
- <textarea name="content" rows="20" cols="80"><?php echo htmlspecialchars($content); ?></textarea><br>
- <input type="submit" value="Save">
- </form>
- <br>
- <a href="?dir=<?php echo urlencode(ltrim(str_replace(BASE_DIR, '', $currentDir), '/')); ?>">Cancel</a>
- </body>
- </html>
- <?php
- exit;
- }
- // Chmod functionality
- if (isset($_GET['action']) && $_GET['action'] === 'chmod' && isset($_GET['target'])) {
- $targetPath = sanitizePath($currentDir . '/' . $_GET['target']);
- if ($targetPath === false || (!is_file($targetPath) && !is_dir($targetPath))) {
- die("Invalid target!");
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $perm = intval($_POST['permissions'], 8); // Read octal input (e.g., 0755)
- if (chmod($targetPath, $perm)) {
- echo "Permissions changed successfully!<br>";
- } else {
- echo "Failed to change permissions.<br>";
- }
- echo '<a href="?dir=' . urlencode(ltrim(str_replace(BASE_DIR, '', $currentDir), '/')) . '">Back to list</a>';
- exit;
- }
- $currentPerm = substr(sprintf('%o', fileperms($targetPath)), -4);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Change Permissions</title>
- </head>
- <body>
- <?php displayBreadcrumbs($currentDir); ?>
- <h2>Change Permissions for: <?php echo htmlspecialchars($_GET['target']); ?></h2>
- <p>Current permissions: <?php echo htmlspecialchars($currentPerm); ?></p>
- <form method="post">
- <label for="permissions">New permissions (octal, e.g. 0755): </label>
- <input type="text" name="permissions" value="<?php echo htmlspecialchars($currentPerm); ?>">
- <input type="submit" value="Change">
- </form>
- <br>
- <a href="?dir=<?php echo urlencode(ltrim(str_replace(BASE_DIR, '', $currentDir), '/')); ?>">Cancel</a>
- </body>
- </html>
- <?php
- exit;
- }
- // Main file listing interface
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Simple PHP File Manager</title>
- <style>
- body { font-family: Arial, sans-serif; }
- table { border-collapse: collapse; width: 100%; }
- th, td { padding: 8px; border: 1px solid #ddd; }
- tr:nth-child(even){background-color: #f2f2f2;}
- </style>
- </head>
- <body>
- <?php displayBreadcrumbs($currentDir); ?>
- <h2>Listing for: <?php echo htmlspecialchars(str_replace(BASE_DIR, '', $currentDir)); ?></h2>
- <table>
- <tr>
- <th>Name</th>
- <th>Type</th>
- <th>Size</th>
- <th>Actions</th>
- </tr>
- <?php
- // List directory contents
- $items = scandir($currentDir);
- foreach ($items as $item) {
- if ($item === '.' || $item === '..') continue;
- $itemPath = $currentDir . '/' . $item;
- $isDir = is_dir($itemPath);
- echo '<tr>';
- echo '<td>' . htmlspecialchars($item) . '</td>';
- echo '<td>' . ($isDir ? 'Directory' : 'File') . '</td>';
- echo '<td>' . ($isDir ? '-' : filesize($itemPath)) . '</td>';
- echo '<td>';
- // If directory, allow navigation; if file, allow editing.
- if ($isDir) {
- echo '<a href="?dir=' . urlencode(ltrim(str_replace(BASE_DIR, '', $itemPath), '/')) . '">Open</a> ';
- } else {
- echo '<a href="?action=edit&dir=' . urlencode(ltrim(str_replace(BASE_DIR, '', $currentDir), '/')) . '&file=' . urlencode($item) . '">Edit</a> ';
- }
- // Both files and directories can have their permissions changed
- echo '<a href="?action=chmod&dir=' . urlencode(ltrim(str_replace(BASE_DIR, '', $currentDir), '/')) . '&target=' . urlencode($item) . '">Chmod</a>';
- echo '</td>';
- echo '</tr>';
- }
- ?>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement