tags:

views:

86

answers:

4

Images and script hosted on the same account (one site).

We know direct link to each image:

$image = "http://site.com/images/full-1091.jpg"

How can we get the size of this file?

Like:

{ do something with $image and get $image_size }
echo $image_size;

Its better to format $image_size like "156,8 Kbytes" or "20,1 Mbytes".

Thanks.

A: 
 echo filesize($_SERVER['DOCUMENT_ROOT']."/images/full-1091.jpg");

note that http://site.com/images/full-1091.jpg is not a file

as for the formatted output ("156,8 Kbytes" or "20,1 Mbytes") try to help yourself and use search. There is a ton of answers to this question already.

Col. Shrapnel
@kiamlaluno it's URL. there is no thing called "file" in the HTTP protocol.
Col. Shrapnel
`if I copy a file on the server, it's not anymore a file` depends on how do you access it.
Col. Shrapnel
@Col. Shrapnel is right. Although `filesize()` can be used with some URL wrappers since PHP 5.
Pekka
I can understand why this answer has gotten 2 down votes. How can you say that site.com/images/full-1091.jpg is not a file? Does that mean that if I upload a file on a server, it's not anymore a file?
kiamlaluno
@kiam if you access something using the `http://` protocol, *it is indeed not a file*. It is a *resource*, and PHP's file functions can deal with resources only through workarounds that are often limited and not optimal for performance (URL wrappers) . If you have files on the local server, never access them using `http://`, use the file path.
Pekka
@Pekka use the file path means "use $_SERVER['DOCUMENT_ROOT']" ?
Happy
@Ignatz it's one option - or use the hard-coded full path (`/path/to/folder/images/image.jpg`)
Pekka
@Pekka thanks. .
Happy
As the server is using a file system, the file copied on a server is still a file. If on HTTP the used term is resource is simply because, when I access http://site.com/images/full-1091.jpg, the server could return me the content of a file, or the output of a script (PHP, ruby, python, etc); in the second case, talking of file is not correct, and that is why a more generic term is used. The correct term to use is not what the OP is debating, and reporting that the correct term to use is resource doesn't reply to his question.
kiamlaluno
@kiamlaluno please stop trying to explain to me things you do not understand. thank you.
Col. Shrapnel
To notice that this answer doesn't reply to what asked from the question; it just report that http://site.com/images/full-1091.jpg is not a file (which is not what the OP asked), and to search on Internet for how to format a file size. @Col. Shrapnel: Who said I was explaining anything to you? There are other users who commented here, and I didn't say to who I was referring the comment. Actually, I was speaking to all but you. About my knowledge, I don't think you can know what I don't understand, or I do understand.
kiamlaluno
+2  A: 

Images and script hosted on the same account (one site).

Then don't use a URL: Use the direct file path and filesize().

Pekka
I would say that this is the correct solution for files local to the same server as a solution for looking up files via url is quite a bit uglier and longer so it should only be used for remote files: http://www.php.net/manual/en/function.filesize.php#92462
Ashaman
+2  A: 

Use filesize function like this:

echo filesize($filename) . ' bytes';

You can also format the unit of the size with this function:

function format_size($size) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($size == 0) { return('n/a'); } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}
Sarfraz
Very good, thank you!
Happy
@Ignatz: You are welcome :)
Sarfraz
A: 
<?php
  $contents=file_get_contents("http://site.com/images/full-1091.jpg");
   // var_dump($contents);
//  $fp =fopen("1.jpeg","w");
  file_put_contents("1.jpeg",$contents);
  $size= filesize("1.jpeg");
  echo $size."in Bytes";
  ?>

Check this it can work for you

Ankur Mukherjee
dude, it is ugliest solution I have ever seen.
Col. Shrapnel
The solution would be correct (except it is saving the content in 1.jpeg, and trying to get the file size of 1.gif), if the PHP code is running is a server that is different from http://site.com. If the PHP code is running on http://site.com too, then it should directly call `filesize()` passing the image file path. As the OP didn't report if the PHP code is running on the same server that contains the image, this solution is valid too.
kiamlaluno
@kiamlaluno you can't read. `Images and script hosted on the same account.(one site).` You'd better limit your hobbies to sci-fi.
Col. Shrapnel
Col. Shrapnel: I am wearing eyeglasses; what is your excuse to answer to a question with "try to help yourself and use search"? Then, the debate of a file not being a file doesn't make entirely sense, if we are not talking of a resource accessed through HTTP protocol.
kiamlaluno
@kiam here: http://stackoverflow.com/faq `Please do look around to see if your question has already been asked (and maybe even answered!) before you ask.` And talking of questions, a developer who cannot tell what is a file, is good for nothing. To understand basic principles is way more important than ability to copy and paste a code cnippet someone wrote for you on SO. For what most of you, SO users, take programming for.
Col. Shrapnel
I don't know if the OP understands what a file; the only thing I can say it doesn't make sense to access a file locale to the server using http://example.com/filename.extension. I think that who asks a question deserves an explanation of why he is doing the wrong thing; simply saying that is wrong without to exactly explain why it is wrong it is like to copy a code snippet without to know what it does.
kiamlaluno