I was thinking of doing a head request with cURL, was wondering if this is the way to go?
5 Answers
The best solution which follows the KISS principle
$head = array_change_key_case(get_headers("http://example.com/file.ext", 1));
$filesize = $head['content-length'];

- 23,240
- 9
- 79
- 115

- 880
- 1
- 8
- 8
-
2This command doesn't return a key 'content-length' for me, here is there array it returns: `'connection' => string 'close' (length=5) 'date' => string 'Tue, 03 Jun 2014 20:55:55 GMT' (length=29) 'server' => string 'Microsoft-IIS/6.0' (length=17) 'x-powered-by' => string 'ASP.NET' (length=7) 'x-aspnet-version' => string '4.0.30319' (length=9) 'cache-control' => string 'private' (length=7) 'content-type' => string 'image/gif'` – Edson Horacio Junior Jun 03 '14 at 20:57
-
2Quick question, why does it need array_change_key_case? – mario Mar 20 '15 at 13:52
-
RE: "This command doesn't return a key 'content-length' for me" - make sure to set the second param to a value greater than 0 to get the headers in a key/value format. http://php.net/manual/en/function.get-headers.php – Levi May 29 '18 at 16:35
-
@EdsonHoracioJunior The remote server which provides the file may not include the information in the headers. Unfortunately, this cannot be fixed by the client. – Melebius Feb 11 '21 at 13:26
I'm guessing using curl to send a HEAD request is a nice possibility ; something like this would probably do :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://sstatic.net/so/img/logo.png');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);
And will get you :
float 3438
This way, you are using a HEAD request, and not downloading the whole file -- still, you depend on the remote server send a correct Content-length header.
Another option you might think about would be to use filesize
... But this will fail : the documentation states (quoting) :
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality.
And, unfortunately, with HTTP and HTTPS wrappers, stat()
is not supported...
If you try, you'll get an error, like this :
Warning: filesize() [function.filesize]: stat failed
for http://sstatic.net/so/img/logo.png
Too bad :-(

- 395,085
- 80
- 655
- 663
Yes. Since the file is remote, you're completely dependent on the value of the Content-Length
header (unless you want to download the whole file). You'll want to curl_setopt($ch, CURLOPT_NOBODY, true)
and curl_setopt($ch, CURLOPT_HEADER, true)
.

- 13,937
- 6
- 46
- 52
Using a HEAD request and checking for Content-Length
is the standard way to do it, but you can't rely on it in general, since the server might not support it. The Content-Length
header is optional, and further the server might not even implement the HEAD method. If you know which server you're probing, then you can test if it works, but as a general solution it isn't bullet proof.

- 115,121
- 27
- 131
- 155
If you don't need a bulletproof solution you can just do:
strlen(file_get_contents($url));

- 151,645
- 95
- 393
- 500
-
6This will work, but it will also download the file first. Presumably, he wants to know the size before fetching it over the network. – troelskn Sep 10 '09 at 09:08