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.
$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.';
}
Related Questions
- Why is it important to store the return value of the mail() function in a variable before using it in an if statement in PHP?
- What are some best practices for rounding decimal numbers to whole numbers in PHP?
- In what scenarios would it be beneficial to use a library like RedBeanPHP for PHP database operations?