How to get file size on an external server?

Asked

Viewed 251 times

1

People wondered if it would be possible to get the size of a file that is hosted on an external server for example: an audio on the Google server www.google.com/audio.mp3 would be like me to get the filesize of this file even though n is on my server with php or javascript ?

1 answer

2

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 great 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.