Re: fopen and fwrite r+

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



# ric@xxxxxxxxxxxxxxxx / 2007-01-13 17:32:00 -0500:
> I can write to the bottom of the file, no problem, but if I want to  
> put new entries at the top of the file I have problems:
> the previous entry is partially overwritten and mangled..
 
you cannot prepend to a file. to do that you need to create a new
file, write the new data in it, and append data from the old file.

class newLiner extends IteratorIterator
{
    function current()
    {
        return $this->getInnerIterator()->current() . "\n";
    }
}

$lines = array('line 1', 'line 2', 'line 3');
$data = new newLiner(new ArrayObject($lines));
$file = new SPLFileObject('file', 'r');
$temp = new SPLFileObject('temp', 'w');

$all = new AppendIterator;
$all->append($data);
$all->append($file);

foreach ($all as $line) {
    $temp->fwrite($line);
}

$file->fflush();
$temp->fflush();
unset($file); unset($temp);

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux