Page 1 of 1

php file size of html url

Posted: Mon Jun 09, 2008 11:25 pm
by shukra
hi can anybody tell me how to get the file size of html url file?

eg. i have url ==> http://www.news.com.au/index.html
i did using fsockopen() function. but it only gets the html or text contents.
images are not taken.

//part of my code [got it from net]
//---------------------------------------------------------
$parsedUrl = parse_url($url);
$host = $parsedUrl['host'];
$fp = @fsockopen($host, '80', $errno, $errstr, $timeout );
if( !$fp ) {
echo "Cannot retrieve $url";
} else {
// send the necessary headers to get the file
fputs($fp, "GET $path HTTP/1.0\r\n" .
"Host: $host\r\n" .
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3\r\n" .
"Accept: */*\r\n" .
"Accept-Language: en-us,en;q=0.5\r\n" .
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
"Keep-Alive: 300\r\n" .
"Connection: keep-alive\r\n" .
"Referer: http://$host\r\n\r\n");

// retrieve the response from the remote server
while ( $line = fread( $fp, 4096 ) ) {
$response .= $line;
}

fclose( $fp );

// strip the headers
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos + 4);
}
//----------------------------------------------------
after this i saved $response in a temp file and get size by function filesize();

thanks in advance!

Re: php file size of html url

Posted: Thu Jun 12, 2008 5:36 am
by JAB Creations
Welcome to the forums but please look at the list of forums before randomly posting questions! I'm surprised this thread hasn't yet been moved.

Have you tried the following?

Code: Select all

$ch = curl_init('http://my.url.com/');
$output = curl_exec($ch);
echo strlen($output);

Re: php file size of html url

Posted: Thu Jun 12, 2008 5:48 am
by Benjamin
You'll need to parse out all the images, javascript files, css files etc and then calculate the size of each one and add them together.

preg_match_all() would be a good start.