Remote file size without downloading file

前端 未结 14 1670
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 03:01

Is there a way to get the size of a remote file http://my_url/my_file.txt without downloading the file?

相关标签:
14条回答
  • 2020-11-22 03:36

    one line best solution :

    echo array_change_key_case(get_headers("http://.../file.txt",1))['content-length'];
    

    php is too delicius

    function urlsize($url):int{
       return array_change_key_case(get_headers($url,1))['content-length'];
    }
    
    echo urlsize("http://.../file.txt");
    
    0 讨论(0)
  • 2020-11-22 03:37

    As mentioned a couple of times, the way to go is to retrieve the information from the response header's Content-Length field.

    However, you should note that

    • the server you're probing not necessarily implements the HEAD method(!)
    • there's absolutely no need to manually craft a HEAD request (which, again, might not even be supported) using fopen or alike or even to invoke the curl library, when PHP has get_headers() (remember: K.I.S.S.)

    Use of get_headers() follows the K.I.S.S. principle and works even if the server you're probing does not support the HEAD request.

    So, here's my version (gimmick: returns human-readable formatted size ;-)):

    Gist: https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d (curl and get_headers version)
    get_headers()-Version:

    <?php     
    /**
     *  Get the file size of any remote resource (using get_headers()), 
     *  either in bytes or - default - as human-readable formatted string.
     *
     *  @author  Stephan Schmitz <eyecatchup@gmail.com>
     *  @license MIT <http://eyecatchup.mit-license.org/>
     *  @url     <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d>
     *
     *  @param   string   $url          Takes the remote object's URL.
     *  @param   boolean  $formatSize   Whether to return size in bytes or formatted.
     *  @param   boolean  $useHead      Whether to use HEAD requests. If false, uses GET.
     *  @return  string                 Returns human-readable formatted size
     *                                  or size in bytes (default: formatted).
     */
    function getRemoteFilesize($url, $formatSize = true, $useHead = true)
    {
        if (false !== $useHead) {
            stream_context_set_default(array('http' => array('method' => 'HEAD')));
        }
        $head = array_change_key_case(get_headers($url, 1));
        // content-length of download (in bytes), read from Content-Length: field
        $clen = isset($head['content-length']) ? $head['content-length'] : 0;
    
        // cannot retrieve file size, return "-1"
        if (!$clen) {
            return -1;
        }
    
        if (!$formatSize) {
            return $clen; // return size in bytes
        }
    
        $size = $clen;
        switch ($clen) {
            case $clen < 1024:
                $size = $clen .' B'; break;
            case $clen < 1048576:
                $size = round($clen / 1024, 2) .' KiB'; break;
            case $clen < 1073741824:
                $size = round($clen / 1048576, 2) . ' MiB'; break;
            case $clen < 1099511627776:
                $size = round($clen / 1073741824, 2) . ' GiB'; break;
        }
    
        return $size; // return formatted size
    }
    

    Usage:

    $url = 'http://download.tuxfamily.org/notepadplus/6.6.9/npp.6.6.9.Installer.exe';
    echo getRemoteFilesize($url); // echoes "7.51 MiB"
    

    Additional note: The Content-Length header is optional. Thus, as a general solution it isn't bullet proof!


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