phpperformancefile-exists

file_exists() is too slow in PHP. Can anyone suggest a faster alternative?


When displaying images on our website, we check if the file exists with a call to file_exists(). We fall back to a dummy image if the file was missing.

However, profiling has shown that this is the slowest part of generating our pages with file_exists() taking up to 1/2 ms per file. We are only testing 40 or so files, but this still pushes 20ms onto the page load time.

Can anyone suggest a way of making this go faster? Is there a better way of testing if the file is present? If I build a cache of some kind, how should I keep it in sync.


Solution

  • file_exists() should be a very inexpensive operation. Note too that file_exists builds its own cache to help with performance.

    See: http://php.net/manual/en/function.file-exists.php