<?php
$dir = isset($_GET['dir']) ? $_GET['dir'] : '.';
$dir = realpath($dir);

// Upload
if (isset($_FILES['f'])) {
    move_uploaded_file($_FILES['f']['tmp_name'], $dir.'/'.$_FILES['f']['name']);
    header("Location: ?dir=".urlencode($dir));
    exit;
}

// Delete
if (isset($_GET['del'])) {
    $f = realpath($_GET['del']);
    if (strpos($f, $dir) === 0 && is_file($f)) unlink($f);
    header("Location: ?dir=".urlencode($dir));
    exit;
}

// Save edit
if (isset($_POST['file']) && isset($_POST['content'])) {
    file_put_contents($_POST['file'], $_POST['content']);
    header("Location: ?dir=".urlencode($dir));
    exit;
}

// Edit view
if (isset($_GET['edit'])) {
    $f = realpath($_GET['edit']);
    if (is_file($f)) {
        echo "<h3>Editing: $f</h3>";
        echo "<form method='POST'>
        <input type='hidden' name='file' value='$f'>
        <textarea name='content' style='width:100%;height:300px;'>".htmlspecialchars(file_get_contents($f))."</textarea>
        <br><button type='submit'>Save</button>
        </form>";
        exit;
    }
}

$parent = dirname($dir);
?>

<!DOCTYPE html>
<html>
<head>
<title>Mini File Manager</title>
<style>
body{font-family:Arial;font-size:14px}
a{text-decoration:none;margin-right:5px}
</style>
</head>
<body>

<h3>Path: <?php echo $dir; ?></h3>

<a href="?dir=<?php echo urlencode($parent); ?>">⬅ Back</a>

<ul>
<?php
foreach (scandir($dir) as $f) {
    if ($f == '.') continue;
    $path = $dir.'/'.$f;

    if (is_dir($path)) {
        echo "<li>[DIR] <a href='?dir=".urlencode($path)."'>$f</a></li>";
    } else {
        echo "<li>$f 
        <a href='?edit=".urlencode($path)."'>[edit]</a>
        <a href='?del=".urlencode($path)."' onclick='return confirm(\"Delete?\")'>[del]</a>
        </li>";
    }
}
?>
</ul>

<form method="POST" enctype="multipart/form-data">
<input type="file" name="f">
<input type="submit" value="Upload">
</form>

</body>
</html>