fwrite / always first line

Discussion in 'PHP' started by kuser, May 25, 2012.

  1. #1
    IS there any way, i can write every new entry of a file always at the begging, not at the end?

    like if i do an fwrite($Handler,$line); , the $line will be put at the end of the file. i want it to be the first
     
    kuser, May 25, 2012 IP
  2. Macaua

    Macaua Greenhorn

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #2
    at fopen put 'w' mode and then use, fwrite($Handler, $line.file_get_contents($file_path));
     
    Macaua, May 25, 2012 IP
  3. iMarcus

    iMarcus Active Member

    Messages:
    122
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Another option would be to use 'r+' for the fopen() mode, this will allow read, write and Prepend ie add to the beginning of the file:)
     
    iMarcus, May 25, 2012 IP
  4. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #4
    Yup, r+ is the easiest way.
    IE:
    
    <?php
        if ($file = fopen($pathToFile, 'r+'))
        {
            if (fwrite($file, $line) !== false)
            {
                // Success
            } else {
                // Failure
            }
        }
    ?>
    
    PHP:
     
    kbduvall, May 25, 2012 IP