PHP fwrite problem (Full Version)

All Forums >> [Web Development] >> General Web Development



Message


Mane -> 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 -> RE: PHP fwrite problem (3/9/2008 15:12:20)

Could you post some code and a link to phpinfo?




Mane -> 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 -> 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>




Mane -> RE: PHP fwrite problem (3/10/2008 17:30:37)

Unfortunately, that doesn't seem to work. (unless it had something to do with making the textarea really big [;)]).
I don't think it's a problem with the fwrite but with the fread a line down. Still have no idea what it is...




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>




Mane -> 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 -> 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.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.0625