file handling for idiots

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
User avatar
mdow6
Forum Newbie
Posts: 3
Joined: Mon Jul 22, 2002 10:26 am

file handling for idiots

Post by mdow6 »

hey anyone wanna help a newbie?

I want to have php write information to a text file. I would like to write to the beginning of a document from a html form. So far Ive only been able to either overerite the document or appened the document. do I need a script that will analysize the size of the information from the form and add that much white space first?

Ive been hacking away and its starting to get ugly over here
could anyone help me with some pointers on completing such a script?

thanks alot, --don .:inept:.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

$fp = fopen($filename, "r+");
fwrite($fp, $contentToWrite);
fclose($fp);
this will open the file $filename and place the file pointer "cursor" at the beginning of the file.
User avatar
mdow6
Forum Newbie
Posts: 3
Joined: Mon Jul 22, 2002 10:26 am

file handling for idiots

Post by mdow6 »

the script above is basically what I have right now, It still seems that using the "r+" read/write mode still overwrites whatever is at the beginning of the file. Im trying to set up a script that i can write information from a form into reverse chronological order, so the most recent entrys to the log are at the top.

should the fwrite function using "r+" not overwrite data already in the filehandle??

-don
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Basically, you will need to do the following:
  • Open the text file
    Read the contents of the text file to a variable
    prepend the new content to the front of the contents of the variable
    write the variable back to the file, overwriting the content with the new content
    closing the file
User avatar
mdow6
Forum Newbie
Posts: 3
Joined: Mon Jul 22, 2002 10:26 am

Post by mdow6 »

gotcha,

thanks for the much needed clarity
User avatar
James Pelow
Site Admin
Posts: 51
Joined: Sat Jun 01, 2002 5:28 am
Location: Killiney, Co. Dublin, Ireland
Contact:

Um..

Post by James Pelow »

Would it not just be easier to have it append to the file and organise the output later?

Code: Select all

if($total <> 0) &#123;
		
			for($sortno = 0; $sortno < $total; $sortno++) &#123;
				$line = explode("||", $file&#1111;$sortno]); 
					if ($line&#1111;3] <> "News:") &#123;
						$array&#1111;] = "$line&#1111;6]||$line&#1111;0]||$line&#1111;1]||$line&#1111;2]||$line&#1111;3]||$line&#1111;4]||$line&#1111;5]||$line&#1111;6]||$line&#1111;7]||$line&#1111;8]||$line&#1111;9]||\n";
					&#125;
			&#125; // End Loop
				rsort($array);
		&#125; // End Total Check
		

		for ($pua=0; $max_fp_dis > $pua; $pua++) &#123;
			$print_art_split = explode("||", $array&#1111;$pua]);
			$time = (date($date_format, $print_art_split&#1111;0])); // Convert the Unix Time Stamp to a Date (Irish Date Format)
&#125;

That's just a snip from phpmac.com but you should get the idea.

-James
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

yeah, i often just append to it, read the file into an array, and then use array_reverse()
paulogv
Forum Newbie
Posts: 2
Joined: Mon May 27, 2002 5:58 am
Contact:

append text to a file

Post by paulogv »

You should try to open the file like this:

Code: Select all

$fp=fopen("nameOfTheFile", "aw");
I guess the 'a' is for append.

Good luck
;)
Post Reply