I need to check if a file is on HDD at a specified location ($path.$file_name).
Which is the difference between is_file() and file_exists() functions and which is better/faster to use in PHP?
I need to check if a file is on HDD at a specified location ($path.$file_name).
Which is the difference between is_file() and file_exists() functions and which is better/faster to use in PHP?
is_file() will return false if the given path points to a directory. file_exists() will return true if the given path points to a valid file or directory. So it would depend entirely on your needs. If you want to know specifically if it's a file or not, use is_file(). Otherwise, use file_exists().
Neither.
is_file() returns true if the file can be read.
file_exists() can return true if the file is a directory.
Note that in some edge cases file_exists() returns true when is_file() does not because of permissions or edge case filesystem issues where is_file() cant determine if its a "regular file".
Speed doesn't matter here because they are not the same and they will trade places in speed depending on circumstances.
I know this post is old but the difference between this functions is not only their behaviours. If you use is_file() to check the existence of big file, more than 2 Go. You will be surprise. File not exists. :( But if you check with file_exists(), that's works.
is_file would be faster if use it with backslash: \is_file. In this case PHP will provide opcache optimization neither file_exists won't.