471,596 Members | 879 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

trouble measuring filesize of gzipped data

My server (Apache/1.3.28 - PHP/4.3.3)
is configured to receive gzipped data via:
Header append Accept-Encoding "gzip, deflate"

PHP is configured to send gzipped data via:
php_value output_handler ob_gzhandler

When downloading files from servers that send gzipped data as well,
I'd like to get the filesize before and after my server decompresses them.

============================================
// same result with fopen()
$file = file_get_contents("http://someUrl");
============================================

strlen($file) yields the decompressed filesize, however.
How can I measure the filesize of the compressed data?
Anyone any idea?
Marek

bn*****@g214mx.net
(remove numbers to despam)
Jul 17 '05 #1
4 2485
"Marek Möhling" <no****@nospam.org> wrote in
news:cu*************@news.t-online.com:
My server (Apache/1.3.28 - PHP/4.3.3)
is configured to receive gzipped data via:
Header append Accept-Encoding "gzip, deflate"

PHP is configured to send gzipped data via:
php_value output_handler ob_gzhandler

When downloading files from servers that send gzipped data as well,
I'd like to get the filesize before and after my server decompresses
them.

============================================
// same result with fopen()
$file = file_get_contents("http://someUrl");
============================================

strlen($file) yields the decompressed filesize, however.
How can I measure the filesize of the compressed data?


This won't be 100% accurate, but you could estimate the gzipped size
using:

strlen(gzcompress($file));

Also, an "I'm not sure that does what you think it does" note. The Apache
directive only applies when people are requesting web pages from your
server. This is good, and can save you some bandwidth, but it doesn't have
any effect on your PHP scripts when they download remote files. Your
scripts are downloading the raw, uncompressed data.

hth
--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources
Jul 17 '05 #2
> This won't be 100% accurate, but you could estimate the gzipped size
using:
strlen(gzcompress($file));
Thanks governor, an obvious solution, I was sitting on my brains...-) I
guess.
Then again, this would give a false result when the remote server was
sending uncompressed data.
Also, an "I'm not sure that does what you think it does" note. The Apache
directive only applies when people are requesting web pages from your
server. This is good, and can save you some bandwidth, but it doesn't have
any effect on your PHP scripts when they download remote files. Your
scripts are downloading the raw, uncompressed data.
IMO when the remote server is configured to sent compressed data,
that's what
a) my server
b) my PHP script
get. I guess that my server handles the decoding and
passes it to my script.

http://web-sniffer.net
detects correctly wether a server uses compression or not.
I checked it by requesting my domain with:
"php_value output_handler ob_gzhandler"
set and unset by htaccess.

So somehow it's possible...
I guess that opening a socket myself using PEAR:Net would be needed,
(bypassing file_get_contents and it ilk) but I can't figure out the
details.


Marek

bn*****@g214mx.net
(remove numbers to despam)



"Senator Jay Billington Bulworth" <f@fung.arg> wrote in message
news:Xn*************************@65.24.7.150... "Marek Möhling" <no****@nospam.org> wrote in
news:cu*************@news.t-online.com:
My server (Apache/1.3.28 - PHP/4.3.3)
is configured to receive gzipped data via:
Header append Accept-Encoding "gzip, deflate"

PHP is configured to send gzipped data via:
php_value output_handler ob_gzhandler

When downloading files from servers that send gzipped data as well,
I'd like to get the filesize before and after my server decompresses
them.

============================================
// same result with fopen()
$file = file_get_contents("http://someUrl");
============================================

strlen($file) yields the decompressed filesize, however.
How can I measure the filesize of the compressed data?


This won't be 100% accurate, but you could estimate the gzipped size
using:

strlen(gzcompress($file));

Also, an "I'm not sure that does what you think it does" note. The Apache
directive only applies when people are requesting web pages from your
server. This is good, and can save you some bandwidth, but it doesn't have
any effect on your PHP scripts when they download remote files. Your
scripts are downloading the raw, uncompressed data.

hth
--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources



Jul 17 '05 #3
"Marek Möhling" <no****@nospam.org> wrote in
news:cu*************@news.t-online.com:
Also, an "I'm not sure that does what you think it does" note. The Apache directive only applies when people are requesting web pages from your server. This is good, and can save you some bandwidth, but it doesn't have any effect on your PHP scripts when they download remote files. Your scripts are downloading the raw, uncompressed data.
IMO when the remote server is configured to sent compressed

data, that's what
a) my server
b) my PHP script
get. I guess that my server handles the decoding and
passes it to my script.
It's sort of the other way around. When your script sends a
request to
another website, that request isn't coming from Apache, it's
coming from
PHP. PHP then sends the result of your script to Apache, and
Apache
sends it to your web browser.
So somehow it's possible...
I guess that opening a socket myself using PEAR:Net would be needed, (bypassing file_get_contents and it ilk) but I can't figure out the details.


There is probably a way to do it via sockets. I played around
but couldn't figure out how to decode the gzipped data properly,
gzuncompress() gave me a data error. I found this suggestion
<http://bugs.php.net/bug.php?id=28051&edit=1> which might help
you out.

hth
--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources
Jul 17 '05 #4
Thanks for your help,
I sorted it out, putting bits & pieces together;
all hail to the internet!
....It helps us to solve trouble we wouldn't have without it %&$§!!!
<?php

// my server sends gzipped data if client allows
$url = "www.byteshift.de";

// see: de.php.net/manual/en/function.gzencode.php
// there might be better versions
function gzdecode($string){
$string = substr($string, 10);
return gzinflate($string);
}

function get_gzipped_data($url){
$http_response = '';
$fp = fsockopen($url, 80);
fputs($fp, "GET / HTTP/1.1\r\n");
fputs($fp, "Accept-Encoding: gzip\r\n");

fputs($fp, "Host: $url\r\n\r\n");
while (!feof($fp))
$http_response .= fgets($fp, 128);
fclose($fp);
return $http_response;
}

preg_match("/^(.+)\r?\n\r?\n\w+\r?\n(.+)$/s",
get_gzipped_data($url),
$matches);
$header = $matches[1];
$body = $matches[2];
$html = gzdecode($body);
$strlen_uncomp = strlen(file_get_contents("http://$url/"));
$strlen_decomp = strlen($body);

echo "
strlen_uncomp: $strlen_uncomp Kb
strlen_decomp: $strlen_decomp Kb
=============================
$html
";
?>
Marek

bn*****@g214mx.net
(remove numbers to despam)
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Phil Powell | last post: by
7 posts views Thread by Michael J. Astrauskas | last post: by
3 posts views Thread by Oxygenearth | last post: by
1 post views Thread by Paul Smith | last post: by
3 posts views Thread by 2good2b | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.