|
| |
|
|
Mane
Posts: 100 Joined: 7/7/2005 Status: offline
|
PHP fwrite problem - 3/8/2008 12:59:07
I'm making a file editor with PHP. The file contents are displayed in a textarea and are saved using fwrite() when the submit button is pressed. After submit is pressed, though, not all of the updated file is shown in the textarea - part is chopped off, and is only shown when the page is reloaded. Is this caused by fwrite not having time to execute before the contents are read again (with fread)? I added a slight delay using usleep(). This improved the situation a bit but a few characters are still sometimes chopped off. I don't want to add a greater delay, and it probably wouldn't be a foolproof solution. What can I do?
|
|
|
|
BobbyDouglas
Posts: 5455 Joined: 5/15/2003 From: Arizona Status: offline
|
RE: PHP fwrite problem - 3/9/2008 15:12:20
Could you post some code and a link to phpinfo?
_____________________________
Arizona Web Design - Mr Bobs Web Design in Arizona The Arizona Web Hosting Challenge
|
|
|
|
Mane
Posts: 100 Joined: 7/7/2005 Status: offline
|
RE: PHP fwrite problem - 3/10/2008 12:38:01
Sure. Here is phpinfo: http://bell.yerich.net/mar10/phpinfo.php Here is the page (I took out a bunch of code unrelated to the problem): http://bell.yerich.net/mar10/editsimple.php Here is the code:
<?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
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($contents);?></textarea>
<br />
<input type="submit" name="submit" value="Save Changes" />
<?php fclose($readfh); ?>
</form>
</body>
</html>
|
|
|
|
BobbyDouglas
Posts: 5455 Joined: 5/15/2003 From: Arizona Status: offline
|
RE: PHP fwrite problem - 3/10/2008 13:36:28
This should work. <?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
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); //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="overflow:auto;width:1000px; height:1000px;"><? echo($contents);?></textarea>
<br />
<input type="submit" name="submit" value="Save Changes" />
<?php fclose($readfh); ?>
</form>
</body>
</html>
_____________________________
Arizona Web Design - Mr Bobs Web Design in Arizona The Arizona Web Hosting Challenge
|
|
|
|
BobbyDouglas
Posts: 5455 Joined: 5/15/2003 From: Arizona Status: offline
|
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>
< Message edited by BobbyDouglas -- 3/10/2008 18:15:07 >
_____________________________
Arizona Web Design - Mr Bobs Web Design in Arizona The Arizona Web Hosting Challenge
|
|
|
|
Mane
Posts: 100 Joined: 7/7/2005 Status: offline
|
RE: PHP fwrite problem - 3/10/2008 18:33:29
So, if I read that right, you're just avoiding reading the contents a second time? It works! Thanks.
|
|
|
|
BobbyDouglas
Posts: 5455 Joined: 5/15/2003 From: Arizona Status: offline
|
RE: PHP fwrite problem - 3/10/2008 19:36:45
You basically force it to read the contents again. Without the first change, it will only display the contents after you post.
_____________________________
Arizona Web Design - Mr Bobs Web Design in Arizona The Arizona Web Hosting Challenge
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|