Reputation: 689
I am trying to build a log parser using the zLib functions, and am running into a problem. This is my code:
$filename = '/Users/awallace/AccessLogs/access.log.6.gz';
$handle = gzopen( $filename, 'r');
while ( $buffer = gzgets( $handle, 2048 ) )
{
if ( strpos($buffer, "Leadbuilder.") !== false )
{
print $buffer . "\n";
}
gzclose($handle);
}
(I have removed error checking code). WhenI run this, I get a warning:
Warning: gzgets(): 5 is not a valid stream resource in /Users/awallace/test.php on line 22
If I dump out the handle after gzopen, I get: "Resource id #5". Any idea why this isn't working?
/Users/awallace/AccessLogs/access.log.6.gz: gzip compressed data, from Unix, last modified: Wed Feb 24 23:35:20 2016
Thanks..
Upvotes: 1
Views: 661
Reputation: 484
You close the handle inside your loop, so on the second loop iteration $handle
is invalid.
Instead do this:
$handle = gzopen( $filename, 'r');
while ( !gzeof($handle) )
{
$buffer = gzgets( $handle, 2048 );
if ( strpos($buffer, "Leadbuilder.") !== false )
{
print $buffer . "\n";
}
}
gzclose($handle);
Upvotes: 2