PHP: Checking if a remote file exists via HTTP

Updated: January 11, 2024 By: Guest Contributor Post a comment

Introduction

When working on a PHP project that integrates external resources, you might often find yourself needing to verify whether a remote file exists over HTTP before attempting to download or link to it. This tutorial will guide you through the process of checking the existence of a remote file using PHP. Ensuring that a file exists can prevent broken links, reduce errors, and improve user experience.

Understanding HTTP Requests

Before we dive into the PHP code, let’s touch upon how HTTP requests work. Every time you access a file or a webpage, either through the browser or code, your client sends an HTTP request to the server where the resource is hosted. The server responds with an HTTP status code that indicates the outcome of the request; for example, 200 for success, 404 for not found, etc.

Using cURL in PHP

cURL is one of the most versatile tools for making HTTP requests in PHP. Its ability to facilitate various request methods, handle cookies, and manipulate headers make it perfect for checking if a remote file exists.

Basic cURL usage

To start, we need to initialize a cURL session and set the required options to make a HEAD request:

<?php
// Remote file URL
$fileUrl = 'http://example.com/file.txt';

// Initialize cURL session
$ch = curl_init($fileUrl);

// Specify that we want to make a HEAD request
curl_setopt($ch, CURLOPT_NOBODY, true);

// Follow any redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// Execute cURL request
$result = curl_exec($ch);
// Getting the HTTP status code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Close cURL session
curl_close($ch);

if ($httpCode == 200) {
   echo 'File exists.';
} else {
   echo 'File does not exist.';
}
?>

Explaining the cURL settings

The CURLOPT_NOBODY option specifies that we’re only interested in the document’s headers, hence the ‘HEAD’ request. This is faster than a ‘GET’ because it does not download the body of the resource.

CURLOPT_FOLLOWLOCATION will make cURL follow any ‘Location’ header for redirects, ensuring you detect the file even after any redirection.

The CURLINFO_HTTP_CODE gives you the HTTP response code which you can use to validate the existence of the file. A response code of 200 means success, indicating that the file does exist.

Example of Using curl_error()

You can use curl_error() to check for errors after executing a cURL request:

<?php

$url = 'http://example.com'; // Replace with the URL you want to access

$ch = curl_init($url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Timeout in seconds

// Execute the cURL request
$response = curl_exec($ch);

// Check for errors in the cURL request
if (curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
} else {
    echo 'Response: ' . $response;
}

// Close the cURL session
curl_close($ch);

?>

Alternative approach: get_headers()

While cURL is robust and versatile, a simpler alternative exists for checking a remote file: the get_headers() PHP function. Here’s how you use it:

<?php
$fileUrl = 'http://example.com/file.txt';

$headers = @get_headers($fileUrl);

if($headers && strpos($headers[0], '200') !== false) {
   echo 'File exists.';
} else {
   echo 'File does not exist.';
}
?>

@get_headers will silence any warning that usually occurs when the file does not exist. If the file exists, the first element of the $headers array should contain the ‘200’ status code. By checking for this, you can determine the presence of the file.

Despite its simplicity, get_headers() might not be ideal for high-performance applications compared to cURL, due to the lack of fine control over the request.

Using get_headers() with Error Handling

Error handling with get_headers() is a bit different in comparison to curl. You can use error_get_last() to get details of the last error that occurred.

<?php

$url = 'http://example.com'; // Replace with the URL you want to access

// Use the '@' operator to suppress warnings
$headers = @get_headers($url);

if ($headers !== false) {
    echo 'Headers: ' . print_r($headers, true);
} else {
    $error = error_get_last();
    echo 'Error: ' . $error['message'];
}

?>


Conclusion

In this tutorial, we’ve learned how to check whether a remote file exists using cURL and get_headers() in PHP. Using HEAD requests to check for the presence of files is a fast and efficient method that can be used in various PHP applications. Always ensure to add proper error handling to avoid unwanted outcomes in your application.

As you integrate external resources through HTTP, use the tools PHP provides and ensure you’re following good error-handling practices. The techniques described here can be applied not just for existence checks, but also for collecting metadata about resources before working with them. With these skills, you enhance the reliability and user experience of your own PHP applications.