is_file or file_exists in PHP

PhpFileExists

Php Problem Overview


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?

Php Solutions


Solution 1 - 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().

Solution 2 - Php

is_file() is the fastest, but recent benchmark shows that file_exists() is slightly faster for me. So I guess it depends on the server.

My test benchmark:

benchmark('is_file');
benchmark('file_exists');
benchmark('is_readable');

function benchmark($funcName) {
    $numCycles = 10000;
    $time_start = microtime(true);
    for ($i = 0; $i < $numCycles; $i++) {
	    clearstatcache();
	    $funcName('path/to/file.php'); // or 'path/to/file.php' instead of __FILE__
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "$funcName x $numCycles $time seconds <br>\n";
}

Edit: @Tivie thanks for the comment. Changed number of cycles from 1000 to 10k. The result is:

  1. when the file exists:

    is_file x 10000 1.5651218891144 seconds

    file_exists x 10000 1.5016479492188 seconds

    is_readable x 10000 3.7882499694824 seconds

  2. when the file does not exist:

    is_file x 10000 0.23920488357544 seconds

    file_exists x 10000 0.22103786468506 seconds

    is_readable x 10000 0.21929788589478 seconds

Edit: moved clearstatcache(); inside the loop. Thanks CJ Dennis.

Solution 3 - Php

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.

Solution 4 - Php

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.

Solution 5 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDuncan BenoitView Question on Stackoverflow
Solution 1 - PhphbwView Answer on Stackoverflow
Solution 2 - PhpYasenView Answer on Stackoverflow
Solution 3 - PhpBradView Answer on Stackoverflow
Solution 4 - PhpJuan - 6510866View Answer on Stackoverflow
Solution 5 - PhpSpinyManView Answer on Stackoverflow