Check whether image exists on remote URL

Check whether image exists on remote URL

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

Stefan Bogdanescu

Founder & Senior Architect

Check Whether Image Exists on Remote URL

Validating remote assets before attempting to display or cache them is a common requirement when building dynamic content systems, such as generating book covers based on ISBNs. The core challenge lies in performing this validation efficiently without incurring significant latency. When processing hundreds of records per batch, as seen with the need to check approximately 60 URLs per book, even a minor delay per request compounds into substantial waiting times that degrade user experience or slow down background jobs.

The most straightforward approach using PHP involves leveraging the cURL library to perform an HTTP HEAD request. Unlike a GET request, which retrieves the full image data and wastes bandwidth, a HEAD request asks the server only for the headers containing metadata about the resource. This method is significantly faster and consumes less memory, making it ideal for high-volume validation tasks.

Here is a practical implementation using cURL to check if an image exists:

function imageExists($url) {
    $ch = curl_init();
    
    // Set options to perform a HEAD request and follow redirects automatically
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);      // Do not return the body
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
    // Set a short timeout to prevent hanging on unresponsive servers
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
    
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // Check for successful redirects (201) or the resource itself (200)
    return $httpCode === 200 || $httpCode === 201;
}

The critical setting here is CURLOPT_NOBODY. When this flag is set to 1, cURL does not download the content body, only the headers. This ensures that a 5MB image file never enters your application's memory stream during the check phase. Additionally, handling HTTP redirects via CURLOPT_FOLLOWLOCATION is essential because many image hosting services return a 302 status code initially, redirecting to the actual image location or a different server. Without following these redirects, your logic might incorrectly report that an image does not exist simply because the initial redirect header was received instead of the final resource status.

Despite its simplicity, relying solely on curl_exec can still introduce latency if the remote server is slow to respond. In scenarios where you need to process 60 URLs per book, sequential execution becomes a bottleneck. To mitigate this, you should wrap the validation logic in an asynchronous job queue system or utilize PHP's asynchronous capabilities like ReactPHP. However, for many legacy systems or simple scripts, parallelizing the requests using pcntl_fork (on Linux) or spawning multiple child processes can drastically reduce total wait time without introducing the complexity of a full event loop.

Another potential pitfall is handling HTTP error codes beyond 200 and 201. A status code of 404 clearly indicates the image is missing, while codes like 403 (Forbidden) or 500 (Internal Server Error) suggest accessibility issues rather than content absence. Your application logic should distinguish between a missing resource and a server error to avoid silently dropping images that might be valid but currently blocked.

If you are working within the Laravel ecosystem, consider integrating this check into your migration scripts or queue jobs using Laravel's Http facade or the underlying Symfony HTTP client. The official LaravelCompany documentation often highlights best practices for handling external API integrations and asset management, which can serve as a reference for structuring these validation routines within modern PHP applications.

Ultimately, the goal is to balance accuracy with speed. By using HEAD requests, managing redirects correctly, and potentially parallelizing the checks, you can ensure that your image generation pipeline remains robust even when dealing with large datasets.

Our network