Here is a function to get the size of a remote file. Note that HEAD requests don’t get the actual body of the request, they just retrieve the headers. So making a HEAD request to a resource that is 100MB will take the same amount of time as a HEAD request to a resource that is 1KB.
function curl_get_file_size($url) {
// Assume failure.
$result = -1;
$curl = curl_init($url);
// Issue a HEAD request and follow any redirects.
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, get_user_agent_string());
$data = curl_exec($curl);
curl_close($curl);
if ($data) {
$content_length = "unknown";
$status = "unknown";
if (preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match("/Content-Length: (\d+)/", $data, $matches)) {
$content_length = (int)$matches[1];
}
// http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if ($status === 200 || ($status > 300 && $status <= 308)) {
$result = $content_length;
}
}
return $result;
}
Usage:
$file_size = curl_get_file_size("https://www.example.com/test.txt");