| ★ wanayoo — archive 1999 http://www.devshed.com/Talk/Forums/Forum5/HTML/002252.html | Nouvelle recherche | Portail wanayoo |
|
|
DevShed Discussion Forums
![]() PHP
![]() file pointer at beginning of file....
|
| next newest topic | next oldest topic |
| Author | Topic: file pointer at beginning of file.... |
|
ROT Junior Member |
The following is taken directly from the PHP quick ref section under fopen. 'r' - Open for reading only; place the file pointer at the beginning of the file. 'r+' - Open for reading and writing; place the file pointer at the beginning of the file. 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. None of these do what I want though one of them say they will. I need to write input from a form to the beginning of a text file. But when I use $myFile = fopen("file.txt","r+"); it writes to the beginning of the file. But when I execute the script again it writes OVER what was previously there. I need it to INSERT the input from the form. Please help! I'm fairly new to PHP so if I've not included some valuable information let me know. Regards, |
|
Shiju Rajan Member |
i think you are try to append the values to the file..If you want to append values to an existing file.then you can use $myFile = fopen("file.txt","a"); ------------------ "The fear of the LORD is the beginning of knowledge..." |
|
ROT Junior Member |
Yeah, that's what I'm using now. BUT, I want to INSERT at the BEGINNING of the text file. None of the parameter's I've tried accomplish this. Is there one that I'm missing? |
|
freebsd Member |
>>Is there one that I'm missing? Ya know what a newline is -> \n |
|
ROT Junior Member |
quote:
|
|
freebsd Member |
>>Not quite sure I follow? This is a common sense and very basic. Okay, let say you open a new text document with notepad, type something, then press ENTER, what does it do? then you are on the 2nd line, that is, the file pointer is now at your 2nd line. So the whole idea is to go downwardly, not upwardly as you thought. the newline ( \n ) is to automated the task of pressing ENTER. So your new record is following line by line from top to bottom. If you set the filepointer to the first line and enter something, of course, your existing first line will get overwritten. And, that first line will NOT move to second line automatically. [This message has been edited by freebsd (edited August 03, 2000).] |
|
ROT Junior Member |
But I want to INSERT the information at the beginning. Say I have 3 lines of information to write to a text file and there are alread lines of text in the file. I want these 3 new lines of text to be INSERTED. Is that not possible? Can I only append? |
|
freebsd Member |
>>But I want to INSERT the information at the beginning Then your existing lines will get overwritten. It's all about file pointer, the existing lines don't move down automatically when there are new lines appending. >>I want these 3 new lines of text to be INSERTED. Once again, if you want to keep your existing lines in that file, use 'a'. >>Is that not possible? |
|
sano Member |
Do what FreeBSD says to do.. Read the file into an array and then add the line you want to be in the beginning of the file to the last cell in the array, then print the array to a file in descending order using something like this: for ($i=count($array); $i>-1; $i--){ Note that this is only an algothrim to give an example. I am not sure if it will work the way it is set up. |
|
Shiju Rajan Member |
ROT, first you open the file in read|write mode.ie ,"r+". then use rewind() to insert your values at the beginning of file(ie,rewind($fp)). --- ------------------ "The fear of the LORD is the beginning of knowledge..." |
All times are MST (US) | next newest topic | next oldest topic |
![]() |
|
Copyright © 1997-2000 ngenuity. All rights reserved.
Powered by: Ultimate Bulletin Board, Version 5.41a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 1999.