13

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 
7
  • 2
    Hmm, interesting. "Zlib support in PHP is not enabled by default." but "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." Not 100% certain what exactly this means. Are you on Windows or *NIX?
    – deceze
    Mar 21, 2012 at 9:42
  • 1
    I am on Windows 7 using an Acquia Dev Desktop installation. Mar 21, 2012 at 9:48
  • Hm, that is interesting. How did you install PHP?
    – Waynn Lue
    Mar 21, 2012 at 9:49
  • In fact, I am learning Drupal using a package available here: acquia.com/products-services/dev-desktop. This package installed PHP with Apache and MySQL. This installation is stable, but may be it did not install ZLib. Mar 21, 2012 at 9:51
  • 1
    @Waynn Before trying that, I want to check whether this is a simple installation issue. I have created another question: stackoverflow.com/questions/9802498/… Mar 21, 2012 at 10:19

3 Answers 3

18

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)); 
} 
5
  • -1 for being factually incorrect. The manual isn't always 100% accurate, and this function certainly is available right now in PHP5. The feature set of PHP6 is far from decided, so your declaration that this function is slated for the next major version is nothing but assumption.
    – Leigh
    Jan 13, 2013 at 13:32
  • and you can prove this how? Jan 14, 2013 at 14:24
  • It was not in my "PHP5" neither that of any of these other posters! Jan 14, 2013 at 14:34
  • 1
    What exactly do I need to prove? I can prove the manual isn't accurate by the fact that it omits version information on a zlib function (as you can see), I can prove the feature set of PHP6 isn't decided because I follow the relevant mailing lists, and can assure you there is no talk of concrete features yet, and I can prove the function is in PHP5 by linking you to the commit that it was introduced in, nearly 3 years ago - Just because it's not on by default, doesn't mean it's not there.
    – Leigh
    Jan 14, 2013 at 14:48
  • 1) Don't use an old version of PHP, 2) Compile it with --with-zlib
    – Leigh
    Jan 15, 2013 at 18:19
5

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)); 
} 
5
  • The library is loaded, but the function does not exist. See my second update on my question. Mar 21, 2012 at 11:00
  • 1
    bugs.php.net/bug.php?id=22123 implies that gzdecode only exists in PHP 6.
    – Waynn Lue
    Mar 21, 2012 at 11:04
  • 2
    @WaynnLue A 10 year old bug report, with a 6 year old comment about PHP6 is not a good reference.
    – Leigh
    Jan 13, 2013 at 13:37
  • @Leigh Any suggestions for a better reference to post? :)
    – Waynn Lue
    Jan 13, 2013 at 21:55
  • As you probably know, the PHP6 project from 6 years ago, is dead. The gzdecode function was finally added to the codebase on 31st May 2010 (you still need --with-zlib so your answer remains correct)
    – Leigh
    Jan 14, 2013 at 8:02
0

Function gzdecode is available since php 5.4enter image description here

My favorite solution is

Uncompress gzip compressed http response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.