php - How to determine the Content-Length of a gzipped file? -
I am currently trying to serve CSS and JS files from a server which does not enable me to mod_gzip or mod_deflate . So I wrote a small PHP script to compress and return the user with Jeezip.
Example Code:
$ filename = "style.css"; If (! File_exists ($ filename) ||! ($ Info = stat ($ filename))) {header ("HTTP / 1.1 404 not found"); Die (); } Header ("Date:". GMDATE ("D, JM YH: I: SE", Time ())); Header ("cache-control: max-age = 2592000"); Header ("Last-Modified:". GMDATE ("D, JM YH: ISE:", $ info ['mtime'])); Header ("etag:" .sprintf ("\"% x-% x-% x \ "", $ info [ino '], $ info [' size '], $ info [' mtime '])); Header ("Accept-range: bytes"); Header ("Cache-Control: Expire". GMDATE ("D, JM YH: I: SE", $ info ['mtime'] + 2592000)); Header ("content-type: text / html"); Ob_start ("ob_gzhandler"); Echo file_get_contents ($ filename); Ob_end_flush (); I have two problems now. First, I'm having trouble determining the size of the compressed file to inform the content-length browser. Generally, I will include this line:
header ("content-length:". $ Info ["size"]); But, if I do, the browser is hanging while trying to wait for more data. Is there a way to calculate the total size? Or should I ignore this header instruction
The second thing is that whenever I look at this PHP file in Firefox, I try to download the results. In Chrome, it just looks like I had any suggestions ?
Edit: Thank you for the SOBOBOX, I replaced it with the end of the code:
header ("content-encoding: gzip" ); $ Compressed = gzencode (file_get_contents ($ filename), 5); Header ("content-length:". Refraction (compressed $); Die ($ collapse); This stuff works great for length! But I'm getting Firefox to download it instead of a file. Edit (
Re-edit: Here is the version of the modified version, Courtesy of Cletus.
// Start buffered output ob_start () Check for // gzip capability if the strip ($ _ server ['HTTP_ACCEPT_ENCODING'], "jizip") == incorrect) {ob_start ("ob_gzhandler"); Echo file_get_contents ($ filename); Ob_end_flush ();} Else echo file_get_contents ($ filename); // Write content length header ('content-length:' .ob_get_length ()); Ob_end_flush (); I'm going to start the new question to understand why the Firefox tries to download the file.
The problem is that to find out the length of the content you need to know that the client's jizip encoding Whether it supports or not, and you have handed that decision using the og_gazandler. From:
ob_start (); Ob_start ('ob_gzhandler'); ... Production of page content ... ob_end_flush (); // a ob_gzhandler header ('content-length:' .ob_get _length ()); Ob_end_flush (); // main one
full version:
$ filename = "style.css"; If (! File_exists ($ filename) ||! ($ Info = stat ($ filename))) {header ("HTTP / 1.1 404 not found"); Die (); } Header ("Date:". GMDATE ("D, JM YH: I: SE", Time ())); Header ("cache-control: max-age = 2592000"); Header ("Last-Modified:". GMDATE ("D, JM YH: ISE:", $ info ['mtime'])); Header ("etg:" .sprintf ("\"% x-% x-% x \ "", $ info [ino '], $ info [' size '], $ info [' mtime '])); Header ("Accept-range: bytes"); Header ("End:". GMDATE ("D, JM YH: ISE:", $ info ['mtime'] + 2592000); Header ("content-type: text / css"); // Note: Was this text / html for some reason? Ob_start (); Ob_start ("ob_gzhandler"); Echo file_get_contents ($ filename); Ob_end_flush (); Header ('content-length:' .ob_get_length ()); Ob_end_flush (); This is a better too than gzip encoding issue itself.
Comments
Post a Comment