php if file exists – How to Check if a File Exists in PHP?

php if file exists – using PHP | file_exists( ), file_exists() function, Use the is_file(), Use the is_readable() function and Use the is_writable() function to check if a file exists and writable.

How to Check If a File Exists in PHP?

php if file exists – It’s an inbuilt function which is used to check whether a file or directory exists or not.

Check if a file exists using the file_exists() function

file_exists ( string $filename ) : bool

Example


Check if a file exists using the is_file() function

index.php


Check if a file exists and readable

index.php

$website_information_file = 'pakainfo.txt';

if (is_readable($website_information_file)) {
	echo 'The file ' . $website_information_file . ' exists and is readable';
} else {
	echo 'The file ' . $website_information_file . ' may not exist or unreadable';
}

Check if a file exists and writable

index.php


Also Read:

Checking if a File Exists in PHP

index.php


Results

The file "pakainfo.txt" exists.
"pakainfo.txt" is indeed a file.
"pakainfo.zip" turned out to be a directory.

I hope you get an idea about php if file exists.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment