How to get the file size of a remotely stored image? (php)

后端 未结 3 2076
闹比i
闹比i 2020-12-21 08:54

Let\'s say we have an image file, stored in a remote server (for example, let\'s take this image), how can we determine (in PHP code) it\'s file size?

If the file wa

相关标签:
3条回答
  • 2020-12-21 09:52

    echo get_headers($url,1)['Content-Length'];

    0 讨论(0)
  • 2020-12-21 09:53

    Assuming you're worried about the size of the file (not the dimensions of the image), you can grab Content-Length and it will usually work.

    If the server on the other end does't supply the header, you'll have no choice but to GET the file, and check its size locally.

    <?PHP
    $headers = get_headers('http://humus101.com/wp-content/uploads/2009/11/Hummus-soup.jpg');
    $size = null;
    foreach($headers as $h){
        /** look for Content-Length, and stick it in $size **/
    }
    if ($size === null){ //we didn't get a Content-Length header
        /** Grab file to local disk and use filesize() to set $size **/
    }
    
    echo "image is $size bytes";
    
    0 讨论(0)
  • 2020-12-21 09:53

    Someone else has a function for that that basically does it as a socket connection: http://snippets.dzone.com/posts/show/1207

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