For certain reasons, I cannot use either mod_deflate or mod_gzip. Therefore, I am trying to implement its work using PHP. There is the following code:

$filecontents = file_get_contents($filename); header('Content-Encoding: gzip'); header('Content-Type: '.mime_content_type($filename)); echo gzcompress($filecontents); 

However, in the end, ERR_CONTENT_DECODING_FAILED takes off in Chrome. What could be the problem? The browser, of course, supports gzip unpacking.

  • Does not work, tried. Everything related to gzip works. And DEFLATE or ZLIB does not work - user64675
  • We are sure that the rest of the code worked without an error, and $filecontents = file_get_contents($filename); header('Content-Type: '.mime_content_type($filename)); echo $filecontents; $filecontents = file_get_contents($filename); header('Content-Type: '.mime_content_type($filename)); echo $filecontents; Does everything return correctly? - E_p
  • Yes, I checked everything. In the end, even replaced by $ handle = fopen ($ filename, "rb"); $ filecontents = fread ($ handle, filesize ($ filename)); fclose ($ handle); C GZIP works, with DEFLATE or ZLIB - no - user64675

2 answers 2

Everything is really simple, only one line is needed.

 <?php ob_start("ob_gzhandler"); ?> <html> <body> <p>Это должно быть сжатой страницей.</p> </body> </html> 

Smoke mana: http://php.net/manual/ru/function.ob-gzhandler.php

    http://php.net/manual/en/function.gzencode.php

     <?php $filecontents = file_get_contents($filename); header('Content-Type: '.mime_content_type($filename)); // mod_gzip // header('Content-Encoding: gzip'); // echo gzencode($filecontents); // mod_deflate header('Content-Encoding: deflate'); echo gzencode($filecontents, 9, FORCE_DEFLATE); 
    • This works, but it is mod_gzip, not mod_deflate - user64675
    • Apparently, Mark Adler has already answered your question in the English version. As for the specs, this is HTTP "deflate" - E_p
    • What he replied is not working - user64675
    • What does it mean does not work? The code in my example is perfectly opened in the browser. Data has been compressed for transmission. What is the problem? - E_p
    • The code for your example is mod_gzip, not mod_deflate. Mark Adler advised gzcompress () as a deflate (which is actually zlib, not deflate). However, on gzcompress browser swears ERR_CONTENT_DECODING_FAILED - user64675