|
|
|
|
|
|
| |
| <?php
//***************************************************************
//** title : An efficient iterative and buffered text file reader
//**
//** story : I once wanted to write a web statistics
//** generator based on parsing the entries of
//** access.log of the Apache server. I used
//** file() to get all the lines in an array.
//** It worked. When I uploaded it on some server
//** I got an out of memmory error from the server.
//** Thats because the file was 18mb. I then
//** wrote this class which loads the text file
//** in chunks of 2048 bytes and has a method
//** for returning one line at a time. A line
//** is ended by a newline ('\n').
//**
//** author : Ioannis Cherouvim
//** e-mail : morales@hack.gr
//** date : 2005-03-22
//*******************************************************
class Reader {
var $file;
var $filename;
var $filesize;
var $step = 2048;
var $buffer = "";
var $bytesRead = 0;
var $delimiter = "\n";
//instantiate reader
function Reader($filename) {
$this->filename = $filename;
}
//open file
function open() {
$this->file = fopen ($this->filename, "r");
$this->filesize = filesize($this->filename);
}
//close file
function close() {
fclose ($this->file);
}
//get next line of text
function readLine() {
while (strpos($this->buffer, $this->delimiter)===false) {
$temp = fread ($this->file, $this->step);
$this->bytesRead += $this->step;
if ($temp) {
$this->buffer .= $temp;
} else {
$temp = $this->buffer;
$this->buffer="";
return $temp;
}
}
$returnBuffer = substr($this->buffer, 0, strpos($this->buffer, $this->delimiter));
$this->buffer = substr($this->buffer, strpos($this->buffer, $this->delimiter) + strlen($this->delimiter));
return $returnBuffer;
}
//check if there are more lines in the buffer
function hasMore() {
return ((($this->filesize - $this->bytesRead) > 0) || (strlen($this->buffer) > 0));
}
}
?> | |
An example use of this class
| <?php
$reader = new Reader("hugefile.log");
$reader->open();
while ($reader->hasMore()) {
$entry = $reader->readLine();
echo "$entry<br>";
}
$reader->close();
?> | | |
|
| filesplit : Split big text files in multiple small ones Categories : PHP, Log Files, Filesystem, PHP Classes | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and
Checks / ACH Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP | | | Class that allows the PHP developer to create and manage UNIX like password files suitable for use as Apache authentication password files.
Categories : HTTP, PHP, PHP Classes, Filesystem | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | | | PHP Based Apache + Mysql Error Log Parser Categories : PHP, PHP Classes, Apache, MySQL, Log Files | | | A File Browser Class.To Read Drives,Directories and Files .Files writing is also possible Categories : PHP, PHP Classes, Filesystem | | | 3 lines of Code to extract Tar, Zip, Gzip etc.. Categories : PHP, Filesystem, PHP Classes, Compression | | | Class: Info on Users, Servers and the running script Categories : PHP, Classes and Objects, User Interface, PHP Classes | | | Easy upload class Categories : PHP Classes, Filesystem, HTTP, PHP | | | Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support Categories : PHP, FTP, Filesystem, PHP Classes, Compression | | | Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text Categories : PHP, PHP Classes, Search, Filesystem | | | Access_user Class - an easy to use system for protecting pages and register users. Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication | | | Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP, Filesystem, PHP Classes | |
|
|
|