How to get size of a file on an external server?

1

Galera wanted to know if it would take the size of a file that is hosted on an external server for example: an audio on google server www.google.com/audio.mp3 would have how do I get the filesize of this same file n being on my server with php or javascript?

    
asked by anonymous 17.06.2016 / 00:47

1 answer

1

You need to have the cURL extension, with which you can make an HTTP HEAD request to the remote server. The answer will let you know how big the file is.

Example:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_URL, $url); //specify the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$head = curl_exec($ch);

$size = curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD);

if(<limit the $size>){
    file_get_contents($url);
}
    
17.06.2016 / 01:58