For php flush - how to disable gzip for specific file?

16,684

Solution 1

I was looking for a solutions for the same issue. This is what worked for me but unfortunately it seams NOT to be a VALID header.

<?
header("Content-Encoding: none");
?>

Solution 2

apache_setenv() is correct. See the documentation.

http://php.net/manual/en/function.apache-setenv.php#60530

apache_setenv('no-gzip', '1');

Your problem is that you turned on output buffering with ob_start(). Comment that out.

I've learned that apache_setenv() is only available with the PHP Apache module. It's not available when using FPM. In that case, you have to use .htaccess to turn off GZip. An example is

https://stackoverflow.com/a/36212238/148844

RewriteRule ^dashboard/index - [E=no-gzip:1]
SetEnvIf REDIRECT_no-gzip 1 no-gzip

The - means NOOP, E means set variable, 1 is the value. After redirects, the variables are renamed and prepended with REDIRECT_.

If the output is still being buffered, check if you are going through a proxy or cache. See if headers like Via: 1.1 varnish or Via: 1.1 vegur are present. They will buffer the response also.

Solution 3

Put this in httpd.conf

# exclude certain page requests (e.g. for requesting getMyFile.php?action=getFile&id=3 as non-compressed)
SetEnvIfNoCase Request_URI getMyFile\.php$ no-gzip dont-vary
Share:
16,684

Related videos on Youtube

Moshe Shaham
Author by

Moshe Shaham

Node.js, Java Javascript, Angular, Typescript

Updated on June 04, 2022

Comments

  • Moshe Shaham
    Moshe Shaham almost 2 years

    I have a ajax call to somefile.php . i want the php script to do a simple task and than send back data to the user, and only than do very time consuming tasks. so i need to flush the output after the first simple task. it doesn't work, probably because i have gzip enables.

    I definitely don't want to disable gzip across all the vhost, and also not in all the folder where somefile.php is. i just want to disable it for this specific file. is that possible?

    EDIT:

    this is what i've included in my apache conf:

    <FilesMatch \.php$>
        SetEnv no-gzip 1
    </FilesMatch>
    

    this is my php script:

    <?php
    $sucesss = @apache_setenv('no-gzip', 1);
    @ini_set('zlib.output_compression', 0);
    @ini_set('implicit_flush', 1);
    
    ob_start();
    
    for($i=0;$i<10;$i++)
    {
        echo 'printing...';
        ob_flush();
        flush();
    
        sleep(1);
    }
    ?>
    

    it doesn't work. i still see all the output together after 10 seconds.

    • noli
      noli about 12 years
      If it's your web server that handles the gzip compression, I think you can't disable it via PHP.
  • B. Martin
    B. Martin about 10 years
    How can you disable it only for certain requests to the same file? e.g.: ajax.php?a=list&id=5 -> with compression ajax.php?a=download&file=image.png -> without compression
  • Chloe
    Chloe about 8 years
    Here is the Content-Encoding specifications: w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5
  • Chloe
    Chloe about 8 years
    @B.Martin Leave the $ off of the regex and include the request parameter. Like ajax\.php\?a=download.
  • Déjà vu
    Déjà vu over 4 years
    header("Content-Encoding: identity"); sounds more correct.
  • WilliamK
    WilliamK over 3 years
    I am using SimpleHTMLDom for scraping which does the job and more, but unfortunately it is returning compressed data. I have a fix for that but it doesn't fit in the function that I am using. So I tried these tips to prevent the page from sending Gzip data but none of the suggestions here work.
  • B. Martin
    B. Martin over 3 years
    @e2-e4 I know that identity is correct but sometimes you have proxy servers or other configurations that you cannot change that interfere an encode all traffic.