Check file existence in php

by Php Ninza on July 14, 2009


Php developers need to do a lot of file operations , Work like cache building, logs etc all required file operations functions in php.

www.php.net/file_exists is a handy tool for existing file on to the webserver.

here’s a example code:-
[php]
< ?php
$file = ‘/path/foo.txt’;
if (file_exists($file)) {
echo "$file exists";
} else {
echo "$file does not exist";
}
?>
[/php]

This Functions returns TRUE if the file or directory specified by the filename exists otherwise it retuns FALSE .

  • phpcollection
    @Sean
    Thanks for stopping by. :)

    Ternary operators are sometime difficult to be grasped by newbie coders and this post was directed to them , However the information is useful
  • Or, use the ternary operator for one line of code:
    echo ( file_exists($file) ) ? "$file exists" : "$file does not exist";
blog comments powered by Disqus

Previous post:

Next post:

BidVertiser