Het Het - 1 month ago 9
PHP Question

How to find the download size of a file on HTTP header called Content-Length?

The question also asks us to find the total download size of a URL. But what if that URL passed into the script just points to a single file resource like a JPG file or a GIF file? Well, for a single file resource we just need to find the size of that particular file and then return it as the answer, and we are done. But, for an HTML document we will need to find the total size of all resources that are embedded and included on the page and return that as the answer

function get_remote_file_size($url) {

$headers = get_headers($url, 1);

if (isset($headers['Content-Length']))

//checks for lower case "L" in Content-length:
if (isset($headers['Content-length']))
return $headers['Content-length'];

}

Answer

The get_headers function accepts a URL as an argument. So, the PHP code to retrieve the “Content-Length” header would look like this

function get_remote_file_size($url) {

$headers = get_headers($url, 1);

if (isset($headers['Content-Length'])) 
       return $headers['Content-Length'];

    //checks for lower case "L" in Content-length:
if (isset($headers['Content-length'])) 
       return $headers['Content-length'];


}