Page 1 of 1

file handling for idiots

Posted: Mon Jul 22, 2002 10:26 am
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:.

Posted: Mon Jul 22, 2002 10:36 am
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.

file handling for idiots

Posted: Mon Jul 22, 2002 11:06 am
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

Posted: Mon Jul 22, 2002 11:40 am
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

Posted: Mon Jul 22, 2002 11:44 am
by mdow6
gotcha,

thanks for the much needed clarity

Um..

Posted: Mon Jul 22, 2002 5:14 pm
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

Posted: Mon Jul 22, 2002 7:01 pm
by hob_goblin
yeah, i often just append to it, read the file into an array, and then use array_reverse()

append text to a file

Posted: Mon Jul 22, 2002 7:42 pm
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
;)