PHP: Get the size of a remote file.

In this article, we will show you how to get the size of a remote file using PHP.

We will also include both a cURL and a non-cURL example.

Both of these solutions will attempt to get the size of the file without actually downloading it.

Be warned that there is no surefire way to get the size of a remote file without downloading it. This method relies on the server in question returning a Content-Length header in the header response. Content-Length is an optional header for servers, which means that it might not always be present.

Using cURL to get the size of a remote file.

If you would like to use PHP’s cURL extension to get the size of a remote file, then you can use the following example:

//URL of the remote file that you want to get
//the file size of.
$remoteFile = '';

//Create a cURL handle with the URL of
//the remote file.
$curl = curl_init($remoteFile);

//cURL request follows any redirects.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

//We want curl_exec to return  the output as a string.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//Set CURLOPT_HEADER to TRUE so that cURL returns
//the header information.
curl_setopt($curl, CURLOPT_HEADER, true);

//Set CURLOPT_NOBODY to TRUE to send a HEAD request.
//This stops cURL from downloading the entire body
//of the content.
curl_setopt($curl, CURLOPT_NOBODY, true);

//Execute the request.

//Retrieve the size of the remote file in bytes.
$fileSize = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);


//Convert it into KB
$fileSizeKB = round($fileSize / 1024);

echo 'File is ' . $fileSizeKB . ' KB in size.';

In the code above, we configured cURL to send a HEAD request to the remote file in question.

We then extracted the “Content-Length” header by using the curl_getinfo function and the CURLINFO_CONTENT_LENGTH_DOWNLOAD option.

Note that if the Content-Length header is not returned by the remote server, curl_getinfo will return a -1 (minus one).

You should also note that this is not a reliable way to check if a remote file exists. This is because curl_getinfo might return -1 even if the resource in question exists.

If you want to check if a remote file actually exists or not, then you should read our guide on checking to see if an HTTP resource exists.

Using PHP’s get_headers function to get the size of a remote file.

If you do not have cURL installed, then you can use PHP’s inbuilt get_headers function:

//URL of the remote file that you want to get
//the file size of.
$remoteFile = '';

//Get the header response for the file in question.
$headers = get_headers($remoteFile, 1);

//Convert the array keys to lower case for the sake
//of consistency.
$headers = array_change_key_case($headers);

//Set to -1 by default.
$fileSize = -1;

//Check to see if the content-length key actually exists in
//the array before attempting to access it.
    $fileSize = $headers['content-length'];


In the PHP example above:

  1. We fetched the response headers for the resource in question by using PHP’s get_headers function. We passed in 1 as the second parameter. The second parameter tells get_headers to parse the response and set each header as an array key.
  2. To keep things consistent, we converted all array keys to lower case by using the array_change_key_case function.
  3. We set the $fileSize variable to -1 by default. If the $fileSize variable is still -1, we will know that the Content-Length header was not returned by the server.
  4. We check to see if the “content-length” key exists in our $headers array. Failing to use isset in this particular scenario could result in an undefined index warning. As stated above, the Content-Length header is optional.
  5. Finally, we printed the size of the remote file in bytes.

If you are wanting to get the size of a local file, then you should check out our tutorial on how to get a file’s size in PHP.