Remote file size without downloading file

前端 未结 14 1679
爱一瞬间的悲伤
爱一瞬间的悲伤 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:35

    I'm not sure, but couldn't you use the get_headers function for this?

    $url     = 'http://example.com/dir/file.txt';
    $headers = get_headers($url, true);
    
    if ( isset($headers['Content-Length']) ) {
       $size = 'file size:' . $headers['Content-Length'];
    }
    else {
       $size = 'file size: unknown';
    }
    
    echo $size;
    

提交回复
热议问题