Re: Writing to files

From: Date: Fri, 11 Jul 2003 19:45:27 +0000
Subject: Re: Writing to files
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-154925@lists.php.net to get a copy of this message
You could try using the file() function. Then loop backward through the array or use array_reverse. -- Rob "Jason Giangrande" <jason@giangrande.org> wrote in message news:1057952096.17202.13.camel@dragon.giangrande.org... > Any ideas on how I can print the lines of my file in reverse order, > then? Does fgets() always process from the beginning of the file even > if you open the file with the pointer at the end? I tried to get the > line count of the file and go through each line of the file backwards > but that doesn't seem to work, so either it's impossible or I'm going > about it the wrong way. Here's the code: > > $fh = fopen("data.txt", "a+") or die("Could not open file"); > $line_num = 0; > while (! feof($fh)) { > if ($line = fgets($fh, 1048576)) { > $line_num++; > } > } > while ($line_num != 0)) { > if ($line = fgets($fh, 1048576)) { > print $line; > $line_num--; > } > } > > Thanks, > Jason > > > On Fri, 2003-07-11 at 15:04, David Nicholson wrote: > > Hello, > > > > > > This is a reply to an e-mail that you wrote on Fri, 11 Jul 2003 at 19:56, > > lines prefixed by '>' were originally written by you. > > > Is there a way to write to a beginning of a file without it > > > overwriting > > > data that's already there or do I have to write to the end of the file > > > in order to preserve data? I ask because it would be much easier to > > > print the lines of the file out in order of last added first if I > > > could > > > add lines at the top of the file. > > > > Not without reading the entire file into a variable first then appending > > that variable to the data you wish to add and saving the entire file again > > (which will obviously take longer than appending to the end of the file). > > > > David. > > > > -- > > phpmachine :: The quick and easy to use service providing you with > > professionally developed PHP scripts :: > > http://www.phpmachine.com/ > > > > Professional Web Development by David Nicholson > > http://www.djnicholson.com/ > > > > QuizSender.com - How well do your friends actually know you? > > http://www.quizsender.com/ > > (developed entirely in PHP) >

« previous php.general (#154925) next »