Jump to content

Recommended Posts

Hi can anybody help me? pls,

 

I want to get the file size of a url for example

http://www.eatinguide.com/index.htm

//my sample code like this

//----------------------------------------------------------------

$site = file_get_contents('http://www.eatinguide.com/index.htm');

print "filesize[" . strlen($site)/1024 . "kb]";

//----------------------------------------------------------------

the problem is, it only display the size of text. how to display the whole size of the url? or website, including the image files?

 

pls pls, help me?

is it possible in PHP or should i have to write some scripts in pearl/cgi?

 

thanks in advance!

shukra

 

Link to comment
https://forums.phpfreaks.com/topic/109521-get-file-size-of-website-or-url/
Share on other sites

You'd have to use regex to parse the code looking for img tags, grab the URL in them, load them all using file_get_contents, check the size in the same way you're doing above (using the FILE_BINARY flag)

 

Also, interesting note on the filesize() page

 

Tip

 

As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality.

 

http://php.net/manual/en/function.filesize.php

You'd have to use regex to parse the code looking for img tags, grab the URL in them, load them all using file_get_contents, check the size in the same way you're doing above (using the FILE_BINARY flag)

 

Theoretically, this should work. But there's so many things that must be taken in consideration that makes it too hard to do. Example- a HTML page can contain iframes. Each iframe has to be parsed by itself. Except that, things like background images (that can be found only in the CSS file most of the times), background audio, flash objects, applets (and the list goes on) all have to be checked for their sizes.

 

 

So bottom line, I don't think PHP is the language for this kind of project. I think the way to go here is launching a web browser and pointing it to the wanted address, and then to check how many bytes were downloaded. Of course it's not as that simple, but that's the base of the way I would use if I had to solve this problem.

 

Orio.

Getting an exact size every time would be near impossible with php, but you could get a fairly close guess with a few simple checks...

 

Look for included files (shtml, css, js)

Parse all files for common image extensions and extract them (jpg, gif, png)

Parse remaining tags/attribs (img, background ect) for anything that may have been missed

 

Loop through extracted files and get sizes.

 

Wouldn't take too long to code, assuming you know your regex. It would be horribly inefficient, and I would never recommend doing it on the fly (caching results?).

 

Your way would be quite a bit more accurate, however.

Getting an exact size every time would be near impossible with php, but you could get a fairly close guess with a few simple checks...

 

Look for included files (shtml, css, js)

Parse all files for common image extensions and extract them (jpg, gif, png)

Parse remaining tags/attribs (img, background ect) for anything that may have been missed

 

Loop through extracted files and get sizes.

 

Wouldn't take too long to code, assuming you know your regex. It would be horribly inefficient, and I would never recommend doing it on the fly (caching results?).

 

Your way would be quite a bit more accurate, however.

 

That wouldn't be enough. The page could contain frames/iframes, each should be checked for everything. Flash and applets should be checked too. Some images could show up a lot of times, but they should be counted only once. And I am sure I missed a lot of things that should be checked too.

This is not built for PHP, although it's not impossible.

 

Orio.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.