Check if a file exists in PHP

During coding, sometimes we need to check if a file exists in a given directory or not before processing it using PHP. So, today we will explain to you how to check if a file exists or not in PHP.

PHP Checking if a file exists in a directory, How to check if file exists in PHP, Checking whether a file exists, php file exists relative path, check if file exists in a folder php, php delete file if exists, check any file exists in folder in php, how to verify if a file exists in php, How to check if file exists and readable in PHP, How to check if file exists and writable in PHP, How to check if file exists and executable in PHP

The four different ways to check a file exists or not

  1. file_exists()
  2. is_readable()
  3. is_writable()
  4. is_executable()

1. file_exists()

Using the file_exists() function, you can check whether a file or directory exists or not. But this function works with the path of the file or directory not works with HTTP URLs. It returns true if the file exists otherwise false. Let’s take an example.

2. is_readable()

The is_readable() function is used to check whether a file or directory exists and is readable or not. You can use this function, when you need to open a file for reading. We need to pass file or directory path as parameter to is_readable() function. It returns true if the file exists and readable otherwise false.

No need to use file_exists() and is_readable() functions together because the is_readable() function will check file_exists() as well.

You can use is_file() or is_dir() with is_readable() to check if it is readable specifically on a file only or specifically on a directory only.

3. is_writable()

By the use of the is_writable() function, you can check whether a file or directory exists and writable or not. It returns true if the file exists and writable otherwise false.

4. is_executable()

Using the is_executable() function, you can check whether a file or directory exists and executable or not. It returns true if the file exists and executable otherwise false.

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

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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