Re: fopen and fwrite r+

From: Date: Sun, 14 Jan 2007 03:40:59 +0000
Subject: Re: fopen and fwrite r+
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-247054@lists.php.net to get a copy of this message
# ric@lostprovince.net / 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

« previous php.general (#247054) next »