tags:

views:

4778

answers:

4

I have:

<?php

$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");

if ($_POST["lastname"] <> "")
{
   fwrite($file,$_POST["lastname"]."\n");
}

fclose($file);

?>

but it overwrites the beginning of the file. How do I make it insert?

+6  A: 

I'm not entirely sure of your question - do you want to write data and not have it over-write the beginning of an existing file, or write new data to the start of an existing file, keeping the existing content after it?

To insert text without over-writing the beginning of the file, you'll have to open it for appending (a+ rather than r+)

$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");

if ($_POST["lastname"] <> "")
{
   fwrite($file,$_POST["lastname"]."\n");
}

fclose($file);

If you're trying to write to the start of the file, you'll have to read in the file contents (see file_get_contents) first, then write your new string followed by file contents to the output file.

$old_content = file_get_contents($file);
fwrite($file, $new_content."\n".$old_content);

The above approach will work with small files, but you may run into memory limits trying to read a large file in using file_get_conents. In this case, consider using rewind($file), which sets the file position indicator for handle to the beginning of the file stream. Note when using rewind(), not to open the file with the a (or a+) options, as:

If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position.

ConroyP
You beat me to it I was writing this out.
Unkwntech
Sorry - I know that feeling!
ConroyP
Thanks. I meant the latter.
Well, isn't it overkill for large file ? I mean, file_get_contents copy all the file in memory, right. It's ok for an ini file, but for a log... In that case, a good all seek would do it, doesn't it ?
e-satis
Good point, answer updated to mention rewind, avoiding reading the whole file into memory.
ConroyP
A: 

If you want to put your text at the beginning of the file, you'd have to read the file contents first like:

<?php

$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");

if ($_POST["lastname"] <> "")
{    
    $existingText = file_get_contents($file);
    fwrite($file, $existingText . $_POST["lastname"]."\n");
}

fclose($file);

?>
SeanDowney
A: 

You get the same opening the file for appending

<?php
$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
   fwrite($file,$_POST["lastname"]."\n");
}
fclose($file);
?>
Eduardo Campañó
+3  A: 

If your file is very large and you cant afford reading all the file and then writing it back after the new inserted data, you can use a file cursor.

<?php
    $fp = fopen('/file.txt', 'a+');

    fseek($fp, 0, SEEK_SET); //MOVES THE CURSOR 0 PLACES FROM START OF THE FILE
    fwrite($fp, $_POST["lastname"]."\n");

    fclose($fp);
?>
Gero
Kudos, that's a nice solution. Also worth noting that rewind($fp); is the same as fseek($fp, 0, SEEK_SET);
Paolo Bergantino