How can I check if a given URL refers to a webpage or a raw file? For now, I check the whole file for containing the string <html> but that is neither effective nor reliable.
$content = file_get_contents($url);
if($content)
{
// is directory
if(strrpos($content, "<html>"))
{
echo $url . " is a folder." . "<br>";
}
else // use raw file...
}
else echo $url . " was not found." . "<br>";
You could get the headers and check for the
content-typeheader. If it containstext/html, it’s a HTML file.See Fetch HTTP response header/redirect status with PHP
This won’t be 100% reliable though – in rare cases, it could happen that the server doesn’t send a
content-typeheader.