Rik Heywood Rik Heywood - 3 months ago 34
PHP Question

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.

RC. RC.
Answer

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

Comments