php file size of html url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shukra
Forum Newbie
Posts: 1
Joined: Mon Jun 09, 2008 11:16 pm

php file size of html url

Post 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!
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: php file size of html url

Post 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);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php file size of html url

Post 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.
Post Reply