By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,693 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,693 IT Pros & Developers. It's quick & easy.

Displaying webpage size?

Ajm113
100+
P: 161
Quick question; How can I get the file size of the web page?
Aug 7 '07 #1
Share this Question
Share on Google+
13 Replies


pbmods
Expert 5K+
P: 5,821
Heya, AJM.

Navigate to the directory and execute an `ls -l` or Get Info in the Finder :P

Alternatively, you might be interested in the filesize() function.
Aug 7 '07 #2

Ajm113
100+
P: 161
Sometimes when I want to try stuff like this in php. They have a simple function out of it! X|

Thanks!
Aug 7 '07 #3

Ajm113
100+
P: 161
Actually it is not working! I thought it would work on the first try, but no. Its not displaying anything! I want it to be able to read the file size of websites.

[PHP]$GetSite = $_GET['url'];

$f = fopen($filesize, 'r');

==HTML==

<tr>
<td>File Size:</td>
<td><? echo filesize($filesize) .' bytes'; ?><hr></td>
</tr>[/PHP]
Aug 7 '07 #4

pbmods
Expert 5K+
P: 5,821
Heya, AJM.

Is $filesize the full path to the file? Try using file_exists(), or file_get_contents() to make sure that the file you're looking for is really there.
Aug 7 '07 #5

Ajm113
100+
P: 161
Well I do have those working now, but they seem not to help me a lot.
Aug 8 '07 #6

kovik
Expert 100+
P: 1,044
Well I do have those working now, but they seem not to help me a lot.
Working how? That the file exists? If so, then filesize doesn't work remotely.

You may want to look into using cURL and it's settings for dealing with headers.
Aug 8 '07 #7

Ajm113
100+
P: 161
Here is how:
[PHP]if(file_exists($GetSite))
{
echo filesize($GetSite) .' bytes';
}else{
echo"<font color=\"#FF0000\">Error: Can't get file size!</font>";
}[/PHP]

I don't know where to look for what you suggested.
Aug 8 '07 #8

pbmods
Expert 5K+
P: 5,821
Heya, AJM.

What would be an example of the value of $GetSite?
Aug 9 '07 #9

Ajm113
100+
P: 161
Three examples:

1. http://www.domain3233.com

2.http://www.domain3233.com/Folder/

3.http://www.domain3233.com/Folder/page.ext

I am trying to get this going so I can have the user be able to see what the page size is on my site.

Just click on the "?" mark after the green url under a result.
Aug 9 '07 #10

pbmods
Expert 5K+
P: 5,821
Heya, AJM.

filesize() only works on local files. Are these files hosted on the same server as your filesize script? Try using the file path instead of the URL.

If these files are hosted on a different server, here's my recommendation:
  • Contact the individual who runs the other site and ask him to set up a filesize script or else provide you with the filesizes, or...
  • Set up a cron script to download the files every night and cache their sizes. Then, instead of calculating the size of each file, you'd use the stored values (how often would the filesize change, anyway).
  • If there's a LOT of files, you might want to store the last (e.g.,) 3 sizes that your script found, and for each file where all three values are the same, only download it, say, once per week instead.
Aug 9 '07 #11

kovik
Expert 100+
P: 1,044
Again, cURL will do what you're after. curl_getinfo() will prove useful.
Aug 10 '07 #12

pbmods
Expert 5K+
P: 5,821
Again, cURL will do what you're after. curl_getinfo() will prove useful.
I think from a design standpoint, what this script really needs is a way to cache filesizes. Suppose there are 10 files that you need to determine the sizes of, and each one averages 1 MB. That means that EVERY time the User loads a page, he has to wait for your server to download 10 MB.

AND, what happens when one User loads the page, and while he's waiting, a second User attempts to load the page? The second User will likely have to wait not only for the server to download all the files for him, but he'll have to also wait for the server to finish downloading the last User's files!

On the other hand, if the server were to automatically cache these filesizes by downloading the files once per week, then it can fetch these saved values instantaneously, plus it saves bandwidth... and how often are these files going to significantly change in size, anyway?
Aug 10 '07 #13

kovik
Expert 100+
P: 1,044
I agree with you, but not from a 'design standpoint,' but more in the way of saving bandwidth, both for the server and the client. It's always a good idea to save results and only update when necessary, but I also think it's a good idea to use cURL in order to accomplish this particular example, along with saving the results. cURL has a lot of options, and I believe you can specify exactly how much of the remote document you would like to retrieve. You can get only the request header, which should have the file size in it, and if not, I'm sure theres a way to do it without having to retrieve the entire file. Files usually give their file size within themselves.
Aug 12 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.