Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Call to undefined function gzdecode()

I am getting a strange error message with the following piece of PHP code (I am not a PHP expert):

if ( $file_loc != NULL ) {
    if ( file_exists($file_loc) ) {
        printf(" file exists");
        $handle = fopen($file_loc, "rb");
        $contents = fread($handle, filesize($file_loc));
        fclose($handle);
        $result = gzdecode($contents);
    }
}

I am basically trying to load text content from a gzipped file. I get the following error:

Fatal error: Call to undefined function gzdecode() in ...\sites\MyScripts\fw2.php on line 80

Yet, when I take a look at documentation, it does not seem like I would need to include an extra library, or am I being wrong? How can I solve this issue?

UPDATE

Following another question to check whether this library is installed on my PC, the answer is yes, it is.

From PHP info:

enter image description here

So this is getting more and more confusing...

UPDATE II

I have tried:

<?php

echo phpversion().", ";

if (function_exists("gzdecode")) {
  echo "gzdecode OK, ";
} else {
  echo "gzdecode no OK, ";
}

if (extension_loaded('zlib')) {
  echo "zlib extension loaded ";
} else {
  echo "zlib extension not loaded ";
}

?>

and I get:

5.2.17, gzdecode no OK, zlib extension loaded 
like image 813
Jérôme Verstrynge Avatar asked Mar 21 '12 09:03

Jérôme Verstrynge


2 Answers

gzdecode is not available unless PHP is complied with zlib. It will possibly be included in PHP 6, according to some sources. Notice in the manual how nearly all functions have given a PHP version number when it became / is available. Oddly, they don't think it needed to display a warning message.

Try this code (works for me) for gzdecode without checksums:

function gzdecode($data) 
{ 
   return gzinflate(substr($data,10,-8)); 
} 
like image 62
user1122069 Avatar answered Oct 22 '22 07:10

user1122069


It's not always installed. From the documentation:

Zlib support in PHP is not enabled by default. You will need to configure PHP --with-zlib[=DIR]

The Windows version of PHP has built-in support for this extension. You do not need to load any additional extensions in order to use these functions.

edit: Since this is the accepted answer still, I edited it to add the function suggested as replacement.

function gzdecode($data) { 
   return gzinflate(substr($data,10,-8)); 
} 
like image 22
Waynn Lue Avatar answered Oct 22 '22 06:10

Waynn Lue