#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    41
    Rep Power
    15
    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,
    Justin Southern
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    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");



    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    41
    Rep Power
    15
    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?
  6. #4
  7. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>Is there one that I'm missing?

    Ya know what a newline is -> n
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    41
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freebsd:
    Ya know what a newline is -> n[/quote]


    Not quite sure I follow? What does that have to do with writing information to the beginning of a text file without writing over other information?
  10. #6
  11. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>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).]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    41
    Rep Power
    15
    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?
  14. #8
  15. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>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?
    It's possible, you can do something before or after inserting your lines, like reading the existing lines and put it in array, then insert the new lines, then put the array back after your new lines, but your logic/idea makes it impossible.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Redmond, WA
    Posts
    218
    Rep Power
    15
    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--){
    write to file
    }

    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.
  18. #10
  19. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    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)).

    ---
    probably you can check it out ftell() and fseek() functions also.
    ----
    see the manual for the details...




    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."

Similar Threads

  1. Pointer problem in opening a file
    By LAKI in forum C Programming
    Replies: 13
    Last Post: February 11th, 2004, 08:27 AM
  2. Pointer problems opening file *formatted*
    By LAKI in forum C Programming
    Replies: 4
    Last Post: February 11th, 2004, 06:06 AM
  3. PHP file upload script trouble
    By casbboy in forum PHP Development
    Replies: 9
    Last Post: February 5th, 2004, 07:42 PM
  4. Overwritng contents of Binary file in C
    By nmsudhan in forum C Programming
    Replies: 3
    Last Post: January 14th, 2004, 06:43 AM
  5. Writing to beginning of file ...
    By PaulGer in forum PHP Development
    Replies: 2
    Last Post: January 13th, 2004, 04:35 PM

IMN logo majestic logo threadwatch logo seochat tools logo