<?php
$directory = "includes";
$files = array_diff(scandir($directory), [".", ".."]);
$selectedFile = "";
$fileContent = "";
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["file"])) {
$selectedFile = basename($_POST["file"]);
if (!in_array($selectedFile, $files)) {
die("Invalid file selection.");
}
$filePath = $directory . "/" . $selectedFile;
echo $filePath;
if (isset($_POST["load"]) && is_readable($filePath)) {
$fileContent = file_get_contents($filePath);
}
if (isset($_POST["save"]) && is_writable($filePath)) {
file_put_contents($filePath, $_POST["content"]);
$fileContent = $_POST["content"];
echo "<p>File saved!</p>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple CMS</title>
</head>
<body>
<form method="post">
<label>Choose a file:</label>
<select name="file">
<option value="">-- Select --</option>
<?php foreach ($files as $file) { ?>
<option value="<?php echo htmlspecialchars($file); ?>" <?php if ($file == $selectedFile) echo "selected"; ?>>
<?php echo htmlspecialchars($file); ?>
</option>
<?php } ?>
</select>
<button type="submit" name="load">Load</button>
</form>
<?php if ($selectedFile) { ?>
<form method="post">
<input type="hidden" name="file" value="<?php echo htmlspecialchars($selectedFile); ?>">
<textarea name="content" rows="20" cols="80"><?php echo htmlspecialchars($fileContent, ENT_QUOTES, 'UTF-8'); ?></textarea><br>
<button type="submit" name="save">Save</button>
</form>
<?php } ?>
</body>
</html>