php - How to flush gzipped part of html page -


i'm trying have page output it's < head > + body stuff, , send browser.

then make long mysql queries , output rest of page.

this works long don't gzip content.

example:

a simplified example of code use works:

<?php ini_set('output_buffering', 'on');  echo "head..wait 3 secs</br>"; ob_flush(); flush();  sleep(3); echo 'tail'; ?> 

see live here no gzip

or i'm trying working:

<?php ini_set('output_buffering', 'on'); ini_set('zlib.output_compression', 'on');  echo "head...wait 3 secs</br>"; ob_flush(); flush();  sleep(3); echo 'bar';                      ?> 

which doesn't work see: here

i need work in application not on pages (some don't need gzip or libraries handle them selves, caldav library example), prefer php solution rather enabling deflate application wide .htaccess

what can make flushing gzipped content work?

i can't tell if going problem since buffering problems can happen if not using gzip (zlib).

firstly may apache has deflate module (mod_deflate or mod_gzip) loaded:
can try disable current script this:

apache_setenv('no-gzip', 1); 

secondly may browser using internal buffer response (around 1kb common ie, newer firefox , webkit browsers, browsers may have more or less).
solved echoing @ least 1kb of blanks example:

echo str_repeat(' ', 1024); 

note when using zlib, echoing 1kb of blanks (uncompressed) , flushing not trigger these browsers parse , render content until ~1kb of compressed data received.

thirdly, if using sessions (either explicit in script or via php.ini session.auto_start) need close session in order output sent:

session_write_close(); 

i assume problem browser buffer , zlib. may need create more 1kb of compressed data browser render it.

-- had tried myself create flushing zlib , believe me lots of text render browser start processing, @ end had echo image file (which compressed) in order work.

ini_set('output_buffering', 'on'); //ini_set('implicit_flush', 'on'); ini_set('zlib.output_compression', 'on'); // default 4kb //ini_set('zlib.output_compression_level', 1);  echo "head...wait 3 secs</br>\n"; echo str_repeat("\n ", 500); // enought work fine without zlib echo '<span style="display:none">'; readfile('path/to/an/image/file.png'); // around 8kb (4kb should enough) echo '</span>'; ob_flush(); flush();  sleep(3); echo 'bar'; 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -