PHP: Call to undefined function gzdecode()

时间秒杀一切 提交于 2019-12-18 04:42:35

问题


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:

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 

回答1:


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)); 
} 



回答2:


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)); 
} 



回答3:


Function gzdecode is available since php 5.4

My favorite solution is

Uncompress gzip compressed http response



来源:https://stackoverflow.com/questions/9801908/php-call-to-undefined-function-gzdecode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!