Andy Wallace
Andy Wallace

Reputation: 689

gzopen throwing exception for existing, valid file

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?

Thanks..

Upvotes: 1

Views: 661

Answers (1)

Here2Help
Here2Help

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

Related Questions