问题
i've been googleing but all i get is getimagesize and filesize.
getimagesize dosent get the KB size just width and height which is not what im looking for.
filesize give me the message Warning: filesize() [function.filesize]: stat failed for
the file in question is 51kb .jpg file
$imgsize=filesize("http://localhost/projects/site/schwe/user/1/1.jpg");
does not work,
how do i accomplish this?
回答1:
You cannot get file size of remote elements, either give a relative path on your system OR do a file_get_contents()
to get contents first
. Thus, instead of http:// , do a filesize('/path/to/local/system')
. Make sure its readable by php process
回答2:
You can't look up the filesize of a remote file like that. It is meant for looking at the filesize of local files.
For instance...
$imgsize = filesize( '/home/projects/site/1.jpg' );
回答3:
filesize()
is the function to use. It might be failing because
- You're trying to get a web address & URL wrappers may not be turned on
- That URL isn't valid.
If you're trying to run filesize()
on a local file, reference the file system path, not some web URL.
回答4:
Or you can also do something like :
$header = get_headers($url);
foreach ($header as $head) {
if (preg_match('/Content-Length: /', $head)) {
$size = substr($head, 15);
}
}
回答5:
filesize
takes the name of the file as argument not a URL and it returns the size of the file in bytes. You can divide the return value with 1024
to get the size in KB.
回答6:
I had the same problem, which i solved like this. I don't know how optimal it is, but it works for me:
getimagesize("http://localhost/projects/site/schwe/user/1/1.jpg");
$file_size = $file[0]*$file[1]*$file["bits"];
来源:https://stackoverflow.com/questions/3805253/php-get-the-kb-size-of-an-image