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.
Howe
The fastest way to check existence of a local file is stream_resolve_include_path():
if (false !== stream_resolve_include_path($s3url)) {
//do stuff
}
Performance results stream_resolve_include_path() vs file_exists():
Test name Repeats Result Performance
stream_resolve 10000 0.051710 sec +0.00%
file_exists 10000 0.067452 sec -30.44%
In test used absolute paths. Test source is here. PHP version:
PHP 5.4.23-1~dotdeb.1 (cli) (built: Dec 13 2013 21:53:21)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies