Skip to content
PHP Logo

How can PHP be used to read external HTML files and determine their status?

To read external HTML files and determine their status using PHP, you can use the file_get_contents() function to retrieve the contents of the external file and then check the HTTP response code using the get_headers() function. This allows you to determine if the file exists and if it is accessible.

php
$url = 'https://www.example.com/externalfile.html';
$html = file_get_contents($url);

if($html !== false){
    $headers = get_headers($url);
    $status = substr($headers[0], 9, 3);
    
    if($status == '200'){
        echo 'The file is accessible and the status is OK.';
    } else {
        echo 'The file is not accessible. Status code: ' . $status;
    }
} else {
    echo 'Failed to retrieve the file contents.';
}