top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find the download size of a file in PHP?

0 votes
788 views
How to find the download size of a file in PHP?
posted Mar 20, 2014 by Manish Tiwari

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Using Content-Length Header
we can actually download the file ourselves and then just get the download size for that URL. we can use cURL. Once we download the resource, we can retrieve the download size using the CURLINFO_SIZE_DOWNLOAD parameter. So, using this approach, we can come up with this code:

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'];

//the code below runs if no "Content-Length" header is found:


    $c = curl_init();
    curl_setopt_array($c, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array('User-Agent: Mozilla/5.0 
        (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) 
        Gecko/20090824 Firefox/3.5.3'),
        ));
    curl_exec($c);

    $size = curl_getinfo($c, CURLINFO_SIZE_DOWNLOAD);

    return $size;

    curl_close($c);

}
answer Mar 21, 2014 by Divya Bharti
...