navigation
a webmaster learning community
     Home    Register     Search      Help      Login    
Sponsors

Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.

Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.

Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax

Search Forums
 

Advanced search
Recent Posts

 Todays Posts
 Most Active posts
 Posts since last visit
 My Recent Posts
 Mark posts read

Microsoft MVP

 

PHP fwrite problem

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> General Web Development >> PHP fwrite problem
Page: [1]
 
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

(in reply to Mane)
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>



(in reply to BobbyDouglas)
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

(in reply to Mane)
Mane

 

Posts: 100
Joined: 7/7/2005
Status: offline

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

(in reply to BobbyDouglas)
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

(in reply to Mane)
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.

(in reply to BobbyDouglas)
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

(in reply to Mane)
Page:   [1]

All Forums >> Web Development >> General Web Development >> PHP fwrite problem
Page: [1]
Jump to: 1





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