How can I the size of an image file from the URL?

后端 未结 5 2042
天命终不由人
天命终不由人 2020-12-16 20:56

Images and script are hosted on the same account (one site), but we know only the URL of the image.

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


        
相关标签:
5条回答
  • 2020-12-16 21:20
    <?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

    0 讨论(0)
  • 2020-12-16 21:31

    Please Note:

    The result of filesize() function are cached! Use clearstatcache() to clear the cache...

    source: http://www.w3schools.com/php/func_filesystem_filesize.asp

    0 讨论(0)
  • 2020-12-16 21:37
     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.

    0 讨论(0)
  • 2020-12-16 21:39

    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]); }
    }
    
    0 讨论(0)
  • 2020-12-16 21:41

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

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

    0 讨论(0)
提交回复
热议问题