files
| From: | Gerard Samuel | Date: | Sat, 08 Sep 2001 05:11:53 +0000 |
| Subject: | files | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-66511@lists.php.net to get a copy of this message | ||
hey, I have a large file and I want to keep the last x row of it. I figured out how to navigate to the point where I want to extract the data, but I cant figure out how to extact from the file current $fp and write out the data to another file.
Think of it like this, when the file gets to be x bytes, get the last 20 lines from it and copy it to another file, so that I could dump the old and replace it witht the new....
Thanks.
<--snip of example code-->
$fp = fopen("./chat/chat.txt", "r+");
$size = filesize("chat/chat.txt");
$count = 0;
while (!@feof($fp)) {
$lines = fgets($fp, 4096);
$count = $count + 1;
}
$count--; // return the correct # of lines in file
echo "Lines: $count<br>";
$offset = round($count - ($count / 4));
echo "Offset: $offset<br>";
$count = 0;
rewind($fp);
while ($count < $offset) {
$lines = fgets($fp, 4096);
$count = $count + 1;
}
echo "New Offset: $count<br>";
$file = fread($fp);
if (!copy($file, $file.'.bak')) {
print ("failed to copy $file...<br>\n");
}
fclose($fp);