Web Analytics Made Easy -
StatCounter write to file.. - CodingForum

Announcement

Collapse
No announcement yet.

write to file..

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • write to file..

    How would you write to a file so that it puts the data in the beginning of the file without erasing (overwriting) whats already in the file?
    ~Designer's Toolz~

  • #2
    PHP Code:
    <?php
    $fp 
    fopen("myfile.txt","a");
    fputs($fp,$data_to_write "\r\n");
    fclose($fp);
    ?>

    additionally, the different php modes:
    '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.

    Comment


    • #3
      I'm not sure that's what DsgnrsTLZAdmin's asking.

      It sounds to me that instead of adding to the end of the file (appending), he'd like to add to the start of the file. This is not quite as easy a task.

      Here is a link that describes the process:
      http://unix.about.com/library/tips/bltip056.htm

      Somebody here says something about using the rewind function:
      http://forums.devshed.com/archive/t-5403

      I haven't come up with the exact code in PHP to accomplish your task, however, after reading the above two links you should have a pretty good idea as to what you need to do.

      Good luck,
      Sadiq.

      Comment


      • #4
        ok then...

        PHP Code:
        <?php
        $result 
        append_to_top("myfile.txt",$data_to_write);

        function 
        append_to_top($data_file,$data) {
           
           
        /* Store the file's present contents */   
           
        $original_data file_get_contents($data_file);

           
        /*Write the new data to the file, followed by the original data */
           
        $fp fopen($data_file,"w+");
              
        fputs($fp,$data_to_write "\r\n");
              
        fputs($fp,$original_data);
           
        fclose($fp);

           
        /* currently returns void */
           
        return();
        ?>
        No error handling.
        Last edited by Celtboy; Feb 24, 2004, 10:17 PM.

        Comment


        • #5
          ill give it a try. ill get back to this thread when i try it.
          ~Designer's Toolz~

          Comment


          • #6
            (I had to make a few changes to the function....make sure you copy the latest).

            -Celt

            Comment

            Working...
            X