|
BobbyDouglas -> RE: PHP fwrite problem (3/10/2008 18:06:57)
|
Change <? echo($contents);?> to <? echo($newcontents); ?> Add $newcontents = $contents; Above if(isset($_POST['submit'])) <?php
$filename="textfile.txt"; //sets file to edit
$readfh = fopen($filename, "r"); //File handle for $filename
$contents = fread($readfh, filesize($filename)); //Reads file, through handle $readfh.
?>
<html>
<head>
<title>File Editor Test</title>
</head>
<body>
<h1>File Editor Test</h1>
<?php
$newcontents = $contents;
if(isset($_POST['submit'])) { //if submit was pressed
$writefh = fopen($filename, "w"); //File handle for $filename
if(get_magic_quotes_gpc()){
$newcontents=stripslashes($_POST['editcontents']);
} //strips unneeded backspaces by magicquotes
else{
$newcontents = $_POST['editcontents'];
}
//NEXT 3 LINES ARE THE PROBLEM SPOT:
fwrite($writefh, $newcontents, strlen($newcontents)); //Saves changes
rewind($readfh); //resets cursor in file
$contents = fread($readfh, filesize($filename)); //Updates $contents
echo("The changes were saved.<br/>\n");
fclose($writefh);
}
?>
Edit the file here:
<form method="post" action="<? echo($PHP_SELF); ?>">
<textarea name="editcontents" style="width:400px; height:150px;"><? echo($newcontents); ?></textarea>
<br />
<input type="submit" name="submit" value="Save Changes" />
<?php fclose($readfh); ?>
</form>
</body>
</html>
|
|
|
|