PHP, reading files, and escaping characters
I am trying to make a PHP script that lets you edit files from a web page, like Geocities. I can view the file fine, but if I save it all the quotes and backslashes get escaped. How can I prevent this?
This is the code I am using:
PHP Code:
<html>
<head>
<title>File editor</title>
<link rel="stylesheet" type="text/css" href="/main.css">
</head>
<body>
<?
$dirname = substr(strrchr(getcwd(), "\\"), 1);
$disp_name = $filename;
if ($mode == 1) {
echo "<h3>Editing: " . $disp_name . "</h3>";
$pFile = fopen($filename, "rb");
$oldContents = fread($pFile, filesize($filename));
fclose($pFile);
?>
<form method="post" action="<?=$PHP_SELF?>?filename=<?=$filename?>&mode=2">
<textarea rows="30" cols="70" name="newContents" wrap="off"><?=htmlspecialchars($oldContents)?></textarea>
<br><br>
<input type="submit" value="Save changes">
<input type="button" value="Reload file" onClick="window.location.href='edit.php?filename=<?=$filename?>&mode=1';">
<input type="button" value="Return to Index "onClick="window.location.href='index.php';">
</form>
<?
}
if ($mode == 2)
{
$pFile = fopen($filename, "wb");
fwrite($pFile, $newContents);
fclose($pFile);
echo "<h3>Saved: " . $filename . "</h3>";
?>
<form method="post" action="<?=$PHP_SELF?>?filename=<?=$filename?>&mode=2" name="a">
<textarea rows="30" cols="70" name="newContents" wrap="off"><?=$newContents?></textarea>
<br><br>
<input type="submit" value="Save changes">
<input type="button" value="Reload file"onClick="window.location.href='edit.php?filename=<?=$filename?>&mode=1';">
<input type="button" value="Return to Index "onClick="window.location.href='index.php';">
</form>
<? } ?>
</body>
</html>