Reputation: 319
I have a PHP script that works a lot with large text files, mostly log files. The problem is that most of the time I only want a section of it, from one split point to another. But having to read a 2GB text file only to get a small portion of it is slowing the process down.
Is there any way I can read only parts of the text without having to read the entire file into memory?
The data is stored like this:
|18.05.2013: some log info here...
|19.05.2013: some log info here...
|20.05.2013: some log info here...
|21.05.2013: some log info here...
|22.05.2013: some log info here...
| etc...
So if I wanted only "19.05.2012" I would still have to read all the other text as well. Is there any way I can read only that part?
P.S. A database is not an option, splitting the files into smaller files is also impractical.
Upvotes: 3
Views: 3228
Reputation: 5620
I think you are looking for fseek.
You will need, however, to format your data in a way that Xth-character is the beginning of the Yth-data. Practically, if every log can have the same length, this may be an efficient way. Otherwise, you will still need to read every single lines to search for it.
Let's imagine (untested, but only to get you started):
function getDataFromFile($fileName, $start, $length) {
$f_handle = fopen($filename, 'r');
fseek($f_handle, $start);
$str = fgets($length);
fclose($f_handle);
return $str;
}
Then:
$fname='myfile.txt';
$DATA_LENGTH = 50;
$wanted_data = 12;
$data = getDataFromFile($fname, $DATA_LENGTH*$wanted_data, $DATA_LENGTH);
I hope this helps.
Upvotes: 2