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 
Community
  • 1
  • 1
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • 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 '12 at 09:42
  • 1
    I am on Windows 7 using an Acquia Dev Desktop installation. – Jérôme Verstrynge Mar 21 '12 at 09:48
  • Hm, that *is* interesting. How did you install PHP? – Waynn Lue Mar 21 '12 at 09:49
  • In fact, I am learning Drupal using a package available here: http://www.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. – Jérôme Verstrynge Mar 21 '12 at 09:51
  • Can you try installing PHP separately? – Waynn Lue Mar 21 '12 at 09:59
  • 1
    @Waynn Before trying that, I want to check whether this is a simple installation issue. I have created another question: http://stackoverflow.com/questions/9802498/php-how-to-check-whether-a-libray-has-been-properly-installed-and-enabled – Jérôme Verstrynge Mar 21 '12 at 10:19
  • I can confirm that PHP 5.4.27 downloaded from windows.php.net doesn't support this function even it has to (because version is > 5.4.0 and zlib is built-in). PHP 5.4.27 on windows 7 behaves against PHP official documentation, so it looks like this is bug. – Vitalii Apr 16 '14 at 11:56

3 Answers3

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)); 
} 
glomad
  • 5,539
  • 2
  • 24
  • 38
user1122069
  • 1,767
  • 1
  • 24
  • 52
  • -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 '13 at 13:32
  • and you can prove this how? – user1122069 Jan 14 '13 at 14:24
  • It was not in my "PHP5" neither that of any of these other posters! – user1122069 Jan 14 '13 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](https://github.com/php/php-src/blob/11d24c1593d6617f73d3f290617bd8994182f4dc/ext/zlib/zlib.c#L711) - Just because it's not on by default, doesn't mean it's not there. – Leigh Jan 14 '13 at 14:48
  • 1) Don't use an old version of PHP, 2) Compile it with `--with-zlib` – Leigh Jan 15 '13 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)); 
} 
Community
  • 1
  • 1
Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
  • The library is loaded, but the function does not exist. See my second update on my question. – Jérôme Verstrynge Mar 21 '12 at 11:00
  • 1
    https://bugs.php.net/bug.php?id=22123 implies that `gzdecode` only exists in PHP 6. – Waynn Lue Mar 21 '12 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 '13 at 13:37
  • @Leigh Any suggestions for a better reference to post? :) – Waynn Lue Jan 13 '13 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](https://github.com/php/php-src/blob/11d24c1593d6617f73d3f290617bd8994182f4dc/ext/zlib/zlib.c#L711) (you still need --with-zlib so your answer remains correct) – Leigh Jan 14 '13 at 08:02
0

Function gzdecode is available since php 5.4enter image description here

My favorite solution is

Uncompress gzip compressed http response

Community
  • 1
  • 1
caiofior
  • 429
  • 4
  • 17