<?php
//my method..
$logcontents = file_get_contents('logfile.txt');
May someone document an example method to make it efficient?
<?php
//my method..
$logcontents = file_get_contents('logfile.txt');
|
|
|
<?php
//initialize the buffer
$buffer = null;
//set xmode to `read`, r = read, rb = read binary, w = write, wr = read write, a = append, x = create and write
$handle = fopen('./logfile.txt', 'r')
or trigger_error('Fatal Error: Could not open file', E_USER_ERROR) && exit(); //we need to ensure validity to prevent an infinite loop with feof()
//iterate through the file until EOF (end of file) is reached
while(!feof($handle)) {
//we allocate and read 4096 bytes (4 Megabytes) of the file into memory at a time until EOF
$buffer .= fread($handle, 4096);
}
//we close the stream wrapper to free resources
fclose($handle);
//do something with the buffer
print "File is " . strlen($buffer) . " bytes.";
?>We can of course utilize fread() to accept the complete chunk of the file at once using filesize($filename) as length parameter, but that will not be very efficient in the end.
Edited by Alexander, 14 September 2010 - 10:14 AM.
bits->bytes
Edited by Alexander, 06 September 2010 - 07:58 PM.
Edited by FireGator, 06 September 2010 - 05:19 PM.
FireGator said:
Nullw0rm said: