Easiest way to grab filesize of remote file in PHP?
Solution 1
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)
.
Solution 2
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'];
Solution 3
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 :-(
Solution 4
If you don't need a bulletproof solution you can just do:
strlen(file_get_contents($url));
Solution 5
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.
meder omuraliev
I began learning front end web development around 04-05, started my first program around 06 with PHP. Currently, I am a Web technologist specializing in full stack development and linux administration specializing with the LAMP stack ( HTML5, CSS3, PHP, Apache, MySQL). I also like to dabble in node.js, meteorjs, Python, django and in general like to mess with new technology/stacks. LinkedIn | [email protected]
Updated on July 09, 2022Comments
-
meder omuraliev almost 2 years
I was thinking of doing a head request with cURL, was wondering if this is the way to go?
-
troelskn over 14 yearsThis will work, but it will also download the file first. Presumably, he wants to know the size before fetching it over the network.
-
Edson Horacio Junior almost 10 yearsThis 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'
-
mario about 9 yearsQuick question, why does it need array_change_key_case?
-
Levi about 6 yearsRE: "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. php.net/manual/en/function.get-headers.php
-
Fellipe Sanches about 5 yearsA note: the float number of output is in Bytes ;-)
-
Melebius over 3 years@EdsonHoracioJunior The remote server which provides the file may not include the information in the headers. Unfortunately, this cannot be fixed by the client.