How to get file size of remote file from googlecode using cURL?

I am trying to get the file size of the remote file "compiler-latest.zip" (googlecode.com) using cURL without downloading it, here is my PHP code:

$url = 'http://closure-compiler.googlecode.com/files/compiler-latest.zip';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // optional
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // optional
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // optional
$result = curl_exec($ch);
$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
print 'Filesize: ' . $filesize . '<br><br>';
print_r($result);

But I get the status "HTTP / 1.1 404 Not Found" with the file size (1379 bytes) of this document 404 404 errors. So, if I set (CURLOPT_NOBODY, 0), it downloads the file and returns its correct file size (currently 3820320 byte). My question is how to get the correct size of the file "compiler-latest.zip" without downloading it?

IMPORTANT: this code works as expected with any other url outside googlecode.com.

0
source share
2

get_headers:

<?php

$headers = get_headers('http://closure-compiler.googlecode.com/files/compiler-latest.zip');

$content_length = -1;

foreach ($headers as $h)
{
    preg_match('/Content-Length: (\d+)/', $h, $m);
    if (isset($m[1]))
    {
        $content_length = (int)$m[1];
        break;
    }
}

echo $content_length;
+2

, ?

+1

Source: https://habr.com/ru/post/1768704/


All Articles