Removing contents of a file after reading it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rayner75
Forum Newbie
Posts: 11
Joined: Sun Feb 20, 2005 4:45 pm

Removing contents of a file after reading it

Post by rayner75 »

Hello, on my site i have a feedback forum
when the submit button is pressed it writes the fields into a text field then writes them out to a reciept.

Here is my code:

Code: Select all

<?php
		$name = $_POST['Name'];
		$email = $_POST['Email'];
		$age = $_POST['age'];
		$eqypt = $_POST['Egypt'];
		$china = $_POST['China'];
		$usa = $_POST['USA'];
		$europe = $_POST['Europe'];
		$rating = $_POST['Rating'];
		$comments = $_POST['Comments'];
		
		
      
      	$SEPARATOR = "::";
      
      	$filename = 'c3042575.txt';
		
      
      	$details = "Name: $name".$SEPARATOR.
       	           "Email: $email".$SEPARATOR.
       	           "Age: $age".$SEPARAT
                           "Egypt? $eqypt".$SEPARATOR.
	           "USA? $usa".$SEPARATOR.
	           "China? $china".$SEPARATOR.
	           "Europe? $europe".$SEPARATOR.
	           "Rating: $rating".$SEPARATOR.
                           "Comments: $comments".$SEPARATOR;      
      
       $file = fopen($filename,"a");
       fputs ($file, $details);
       fclose($file);      
      
       $file = fopen($filename,"r");
       $contents = fread($file, filesize($filename));
      
       $arrayContents = explode($SEPARATOR, $contents);      
      
       for ($i=0; $i<count($arrayContents); $i++)
       {
         echo ($arrayContents[$i]."<br />");
       }
?>
When i have read from the text file and displayed its contents how do i clear the text field?

Thanks for the help
Andy


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

If you want to clear the page, you can't. You're echo'ing the information directly to the browser. There's no real way to clear it other than to refresh the page or to redirect to another page. You may use the header() function in PHP (don't forget the output buffers) or <meta> tags.

If you want to clear the file, do this:

Code: Select all

<?php
...
$handle=fopen($file, "w");
fwrite($handle, "");
fclose($handle);
?>
And...boom, you're done. Good luck.


Good luck!


feyd | :roll:
rayner75
Forum Newbie
Posts: 11
Joined: Sun Feb 20, 2005 4:45 pm

Post by rayner75 »

thanks it was to clear the text file :)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Glad to help, sorry feyd. Didn't know the PHP tags were back.
Post Reply