This is a very very quick and very very dirty example of how one could write a file editor.
It performs a search and replace on a file and allows the user to edit and save the result. BE WARNED: this is in no way good coding, secure or fit for any purpose other than being a quick example of how to write something like this in 15 minutes.
--- Start edit.php example:
<html
<head>
<title>Quick and (very very) dirty edit with search and replace</title>
<meta name="author" value="Daan Weddepohl - daan@systonique.nl">
</head>
<body>
<?php
// Save the file
if ($action == "Save") {
if (($fh = fopen("$file", "w")) == false) {
die ("Can't open this file for writing!");
}
//Save result
fputs ($fh, "$result");
echo "<b>There's a chance that the file was actually saved.</b>";
fclose($fh);
}
?>
<form name="loadForm">
<h3>Open a file and perform a search and replace</h3>
<p>File: <input type="text" name="file" value="<?php print "$file"?>">
Search: <input type="text" name="search" value="<?php print "$search"?>">
Replace: <input type="text" name="replace" value="<?php print "$replace"?>"></p>
<input type="submit" value="Go!">
</form>
<hr>
<h3>Result</h3>
<form>
<p>
<input type="hidden" name="file" value="<?php print "$file"?>">
<textarea name="result" cols="80" rows="20" wrap="VIRTUAL">
<?php
// Show the result
if (file_exists("$file")) {
if (($fh = fopen("$file", "r")) == false) {
die ("Aargh!");
}
while (! feof($fh)) {
// Get a line from the file
$line = fgets($fh, 4096);
// Search and replace
$line = ereg_replace("$search", "$replace", $line);
// Make sure that we can even edit files that include the "</textarea> tag"
$line = ereg_replace("</textarea>", "</textarea>", $line);
$content .= "$line";
}
fclose($fh);
echo "$content";
}
?>
</textarea>
</p>
<p>Check out the result before saving it!</p>
<input type="submit" name="action" value="Save">
</form>
</body>
--- End example
I tested it only once and it appeared to be working, but don't blame me if it wreaks havoc on your system.
Good luck!