The following gets the job done, its a) not secure, b) a little redundant.
So you can clean it up as you see fit.
Also, Im not the greatest with Java Script so the :
if(file_exists("$filename")) {
echo "<script>result = confirm(\"Overwrite current '$filename'?\");
if(!result) history.go(-1);</script>";
}
code works, but if the user hits cancel, they get taken back one page, but the file still saves.. so perhaps a simple "die;" somewhere will solve that, im not sure though..
to open a file with the editor.. itd be editfile.php?file=FILENAME.extension...
I.E. you are trying to edit menulayout.php
http://ursite.com/editfile.php?file=menulayout.php
<?
global $PHP_SELF, $submit, $filename;
function save_file() {
global $filename, $filebody, $PHP_SELF;
if(file_exists("$filename")) {
echo "<script>result = confirm(\"Overwrite current '$filename'?\");
if(!result) history.go(-1);</script>";
}
if($file = fopen("$filename", "w")) {
fputs($file, $filebody);
fclose($file);
echo "Filed saved successfully!";
}
else {
echo "Sorry, there was an error saving the file '$filename.'";
}
}
$file = $_GET['file'];
$filename = $file;
$submit = $_POST['submit'];
// Make sure they aren't trying to edit this page or the 'create new file' page.
if ($file == "editfile.php" || $file == "newfile.php") {
die ("Sorry, you cannot edit '$file'!");
}
if($file = fopen("$filename", "r")) {
$filebody = implode("", file($filename));
$filebody = htmlspecialchars($filebody);
fclose($file);
}
// If Form was submitted
if ($submit == "Edit File!") {
$filebody = $_POST['file'];
$filename = $_POST['filename'];
if(!empty($filebody) && !empty($filename)) { // If there is content and a filename
save_file();
}
else { // if they are missing either content or a file name
echo "There was an error saving the file!";
}
}
?>
<html>
<header>
<title> Edit File Page! </title>
</header>
<body>
<br><br><br>
<center>Edit your file below. You can either save it over the old one or save it as a new file (using the filename input box)<br>
BE SURE TO INCLUDE THE EXTENTION.. (.html, .txt, .php.. etc..)<br>
<br><br>
</center>
<form method="post" action="<? echo $PHP_SELF; ?>">
<center><TEXTAREA name="file" cols="80" rows="35">
<? echo $filebody ?>
</textarea>
<br>
File name:<br>
<input type="text" name="filename" value="<?php echo $filename ?>"><br>
<input type=submit name="submit" value="Edit File!"> <input type="reset" value="Reset File">
</form>
</center>
</html>