How to know whether a file exists in URL using PHP

In PHP, to find whether a file exists in disk or on server we have:

if(file_exists($filepath))

However is there a way to know whether a file exists in an url, such as, https://www.google.co.in/images/nav_logo242.png?

Yes, there is. In order to know whether a file exists in URL using PHP, there is:

$headers=get_headers($fileurl);
if(strpos($headers[0], '200 OK') !==false ) {
echo 'File Exists';
}

Try printing $headers to check more details about the file. It’s useful.

Leave a Reply