Remote file size without downloading file

Remote file size without downloading file

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Remote file size without downloading file

Determining the size of a remote resource without transferring its full contents is a standard requirement for robust web applications. Whether you are building a progress bar for a large file upload, implementing intelligent caching strategies, or validating content length before processing, fetching the Content-Length header is the correct approach. You generally do not want to download the entire payload just to see how many bytes it contains.

The HTTP protocol was designed specifically to handle this scenario through metadata headers. The most direct method is using HEAD requests instead of GET requests. While a GET request retrieves the body of the resource, a HEAD request follows the exact same rules but returns only the response headers. This allows your application to inspect the Content-Length header to determine the file size in bytes without ever populating the memory or disk with the actual file data.

Implementing HEAD Requests

In many languages and frameworks, making a HEAD request is as simple as switching the HTTP method. For example, using curl, you can query the headers of a remote resource like this:

curl -I http://my_url/my_file.txt

The -I flag tells curl to perform an inspection (HEAD) request. The response will include a line similar to Content-Length: 1024, which you can parse directly. If the header is missing, it often indicates that the server does not support range requests or dynamic content sizing, which is a significant limitation you must account for in your architecture.

In PHP, the logic remains straightforward using the cURL extension:

$ch = curl_init('http://my_url/my_file.txt');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Optional, depending on parsing needs
curl_setopt($ch, CURLOPT_HEADER, true);         // Required to access headers if not using custom parsers

curl_exec($ch);
$headers = curl_getinfo($ch);
$size = $headers['content_length'] ?? 0;

curl_close($ch);
echo "File size: {$size} bytes";

Handling Modern Frameworks like Laravel

If you are working within the Laravel ecosystem, the HEAD method is supported natively via the HTTP facade or controllers. You can easily construct a request that inspects headers without loading the body:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

$response = Http::head('http://my_url/my_file.txt');

if ($response->successful()) {
    $size = $response->header('Content-Length');
    
    // Handle cases where the header might be null for dynamic streams
    if (!$size) {
        throw new \Exception('Server does not provide Content-Length');
    }
}

It is crucial to handle scenarios where the server omits the Content-Length header. This frequently happens with streaming responses or when the server determines the size dynamically at the moment of request (e.g., generated PDFs or dynamic HTML). In such cases, relying solely on a pre-request check will yield null. You may need to fall back to downloading a small chunk of the file or handling the stream directly if the exact size is unknown.

Trade-offs and Pitfalls

While fetching the header is efficient, it is not universally accurate. Some servers configure themselves to return headers only after receiving a full request, or they might strip specific headers for security reasons. Additionally, proxies and load balancers can sometimes normalize responses in ways that hide the original Content-Length from upstream clients.

Another common pitfall is assuming that an empty response body means a zero-byte file. An empty response could also mean the HEAD request itself failed silently or returned a redirect without headers. Always validate the HTTP status code alongside the header value. If you require guaranteed accuracy and the server is unreliable, downloading the first few kilobytes of the file might be the only viable option, though this incurs some data transfer cost.

For developers integrating with LaravelCompany solutions or building scalable pipelines where resource estimation is critical, understanding these HTTP nuances ensures that your applications remain resilient against missing metadata. Always prefer HEAD requests first; use GET with a small range request (Range: bytes=0-999) only when the header proves unavailable.

Our network