How can one check to see if a remote file exists using PHP?

How can one check to see if a remote file exists using PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Checking Remote File Existence in PHP Without Blocking Your Response

Attempting to determine if a remote file exists by opening it and immediately closing it is a performance anti-pattern that will noticeably slow down your page load times. The source material correctly identifies this pitfall: using an if (fclose(fopen($url))) pattern forces the underlying HTTP client or socket layer to initiate a full TCP handshake, resolve DNS, establish a connection, send an HTTP request header, wait for a response line, and then tear down the connection. Even if the server responds with a 404 immediately, the latency incurred by establishing the network connection remains. For an application rendering a list of websites with favicons, this synchronous blocking call per item will create a linear degradation in performance as the list grows.

To solve this efficiently, you need to rely on HTTP headers rather than file content. The standard approach is to send an HEAD request instead of a GET request. A HEAD request asks the server for metadata about the resource without requiring the body of the response. This means the browser or PHP script never has to download the image data, only the status line and headers. If the server returns a 200 OK, the file exists. If it returns anything else—like 404 Not Found or 403 Forbidden—you can safely assume the specific resource is missing without incurring the cost of downloading bytes.

In pure PHP, you can utilize the built-in cURL extension to handle this gracefully. The key is to disable the automatic output of the response body and strictly check the HTTP status code returned by the request. Here is a pattern that avoids loading the file content while verifying existence:

$url = 'https://example.com/favicon.ico';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, true); // Crucial: Do not download the body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // Not needed for HEAD but good practice
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Handle redirects automatically

// Execute the request synchronously or asynchronously
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode === 200) {
    // File exists, use the remote image
    echo '<img src="' . $url . '">';
} else {
    // File does not exist, use fallback
    echo '<img src="/fallback-icon.png">';
}

The critical configuration here is CURLOPT_NOBODY. When this flag is set to true, cURL sends the request but explicitly tells the remote server that you do not intend to read the response body. This keeps the data transfer minimal, limiting the operation to just the status code exchange. This approach aligns with how modern web performance tools work; they don't download images to check if they exist, they inspect headers.

There are trade-offs to consider regarding implementation details. First, you must ensure your PHP installation has the curl extension enabled (extension=curl). If you are operating in a restricted environment where cURL is unavailable, you might need to parse strings returned by file_get_contents() with default_context, but be aware that this often defaults to GET requests unless you configure the stream context specifically.

Secondly, consider how you handle redirects. The default behavior of cURL follows redirects, which is usually desirable for favicons since they often live at a specific path (e.g., /favicon.ico) or are redirected from the root URL. However, if your fallback logic relies on knowing exactly which server responded to determine availability, following redirects might mask the origin of the error. In the code above, CURLOPT_FOLLOWLOCATION handles this, but you should be aware that a 301 or 302 redirect counts as a successful connection unless the final destination returns an error.

Finally, timing matters. A HEAD request is faster than a GET, but it still incurs network latency. If you are building a dashboard with hundreds of external links, loading them sequentially in PHP will block the main thread. In such cases, offloading these checks to a background job or using non-blocking I/O strategies becomes necessary to keep the user interface responsive. For simple lists where performance is acceptable, the HEAD request method remains the most robust and efficient way to verify remote file existence without bloating your response time.

Our network