Re: Writing to files
| From: | CPT John W. Holmes | Date: | Fri, 11 Jul 2003 19:47:02 +0000 |
| Subject: | Re: Writing to files | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-154924@lists.php.net to get a copy of this message | ||
> 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--;
> }
> }
$file = file('data.txt');
$rev_file = array_reverse($file);
foreach($rev_file as $line)
{ echo $line; }
---John Holmes...