Skip to content

How to check if file exists in PHP?

Last updated on December 13, 2015

If you find yourself doing any kind of disk-based I/O with PHP, you’ll undoubtedly come across the need to verify that a file exists before continuing to read/write against it.

We can use file_exists() to check whether a file exists or not, but keep in mind that this does not work properly with HTTP URLs. This function expects Path to the file or directory parameter. It will Returns TRUE if the file or directory specified by filename exists; FALSE otherwise. This function available from PHP 4 and also available in PHP 5, PHP 7.

Example : PHP – Checking whether a file exists


The result of file_exists() is cached, which means you first need to call the clearstatcache() function if you want to be absolutely sure a file exists.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments