How to check if a file exists from a URL in PHP

Today we’ll explain to you how to check if a file exists from a URL in PHP. Using php built-in file_exists() function, we can check whether a file or directory exists on the server or not but will not use it to check if a file exists on a remote server or not.

Sometimes we need to check the given image URL or another file URL that exists or not so this will help you.

Different ways to check file exists on remote server or not

  1. Using fopen() function
  2. Using get_headers() function
  3. Using cURL function

1. Using fopen() function

Here, we will create a custom function to check if a file exists on a remote server or not using fopen() function in PHP.

2. Using get_headers() function

Also, you can check if the URL is working or not by using the below custom function.

3. Using cURL function

You can do the same task using cURL in PHP as shown in the below code:

That’s it for today.
Thank you for reading. Happy Coding..!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *