How to check if a file exists from a url

How to check if a file exists from a url

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

Stefan Bogdanescu

Founder & Senior Architect

How to check if a file exists from a URL

When you need to verify the presence of a resource on a remote server, local PHP functions like is_file() or file_exists() will immediately fail because they operate strictly within your application's working directory. These utilities cannot inspect network paths or external domains. To resolve this, you must interact directly with the HTTP protocol or leverage a dedicated extension designed for remote file system operations.

The most common approach involves sending an HTTP request to the target URL. If the server responds with a 200 OK status code, the resource likely exists and is accessible. A 404 Not Found response indicates the path is incorrect or the resource has been removed. This method is straightforward but carries significant overhead. Every check requires establishing a TCP connection, performing the TLS handshake (if HTTPS), sending headers, and receiving the status line before you can even determine existence. In high-frequency scenarios, this latency adds up quickly.

Using cURL for Robust Checks

If your environment has the curl extension installed, it provides the most control over HTTP interactions. You can configure a request to follow redirects automatically, which is crucial because a file might be moved behind a 301 redirect. Without following redirects, you would incorrectly assume the file doesn't exist if the original path returns a redirect status.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://example.com/path/to/file.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Do not output directly
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Handle redirects

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode === 200) {
    echo 'File exists.';
} else {
    echo 'File does not exist or is inaccessible.';
}

This approach explicitly checks the HTTP status code returned by curl_getinfo(). It handles the complexity of network layers for you. However, remember that checking existence via HTTP status does not guarantee the file is readable in the way a local filesystem check would. A server might return 200 but still block specific content types or require authentication headers that your script isn't sending.

Leveraging the Fileinfo Extension

For a more lightweight solution without opening a full HTTP transaction, the fileinfo extension offers a dedicated function called finfo_file(). This function accepts a URL string directly and attempts to retrieve metadata about the remote file, including its MIME type. If the file is missing or inaccessible, it throws a FileAccessException.

try {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, 'https://example.com/path/to/file.txt');
    
    if ($mime_type) {
        echo "File exists. Type: $mime_type";
    } else {
        echo "Could not determine file type.";
    }
} catch (FileAccessException $e) {
    echo "File does not exist or cannot be accessed.";
} finally {
    finfo_close($finfo);
}

This method is often faster than cURL because it doesn't require managing the entire request lifecycle manually. However, it relies on the server sending specific headers that allow MIME type detection. If the remote server blocks this metadata or returns a generic error page, finfo_file() might fail even if the resource exists. Additionally, you must handle the FileAccessException carefully to distinguish between network errors and actual file absence.

Performance Considerations

Choosing between these methods depends on your specific constraints. If you are building a tool that checks thousands of URLs per minute, the overhead of cURL might be acceptable if accuracy regarding redirects is paramount. The finfo_file() approach is superior for read-only checks where MIME type detection is secondary to mere existence verification, provided the server configuration supports it.

Always remember that checking file existence over HTTP is inherently different from checking a local path. You are relying on the remote server's willingness to report its status. A 200 response confirms accessibility at the application layer, but it does not confirm the physical presence of the file in the same way file_exists() does locally. If you need deeper integration with remote storage systems or specific cloud providers, look into their dedicated SDKs, which often provide optimized methods for these checks that go beyond standard HTTP interactions. For more advanced PHP utility patterns, resources like https://laravelcompany.com can offer insights into building robust application architectures.

Our network