I decided to run a test to see if it was true that this function, despite it's entirely different purpose, could be used as a faster more efficient file_exists. I even tested against a scenario where the current path being sought was not part any include path currently set on the system, and both functions performed as if they were intended to provide whether a file existed or not. Here is the test code (its a bit of a mess, but it gets the job done):
<?php
echo "Current Include Path: " . get_include_path() . "<br /><br />\n\n";
$st = microtime(true);
$data = file_exists('Templates/Styles/main.css');
$et = microtime(true);
var_dump($data);
echo " <strong>FROM</strong> file_exists('Templates/Styles/main.css')<br />\n";
echo "Completed in : " . ($et - $st) . " seconds<br />\n";
$st = microtime(true);
$data = stream_resolve_include_path('Templates/Styles/main.css');
$et = microtime(true);
var_dump($data);
echo " <strong>FROM</strong> stream_resolve_include_path('Templates/Styles/main.css')<br />\n";
echo "Completed in : " . ($et - $st) . " seconds<br />\n";
$st = microtime(true);
$data = file_exists('Templates/NotADir/NoFileHere.php');
$et = microtime(true);
var_dump($data);
echo " <strong>FROM</strong> file_exists('Templates/NotADir/NoFileHere.php')<br />\n";
echo "Completed in : " . ($et - $st) . " seconds<br />\n";
$st = microtime(true);
$data = stream_resolve_include_path('Templates/NotADir/NoFileHere.php');
$et = microtime(true);
var_dump($data);
echo " <strong>FROM</strong> stream_resolve_include_path('Templates/NotADir/NoFileHere.php')<br />\n";
echo "Completed in : " . ($et - $st) . " seconds<br />\n";
?>
On my server I got the following results:
Current Include Path: .:<!--REMOVED-->/lib/php:<!--REMOVED-->/lib/php
bool(true) FROM file_exists('Templates/Styles/main.css')
Completed in : 4.1E-5 seconds
string(51) "<!--REMOVED-->/Templates/Styles/main.css" FROM stream_resolve_include_path('Templates/Styles/main.css')
Completed in : 2.19345092773E-5 seconds
bool(false) FROM file_exists('Templates/NotADir/NoFileHere.php')
Completed in : 4.05311584473E-6 seconds
bool(false) FROM stream_resolve_include_path('Templates/NotADir/NoFileHere.php')
Completed in : 2.19345092773E-5 seconds
Not only does this show that this is a great replacement for file_exists, it also performs twice as fast. I ran this a few times and under most tests stream_resolve_include_path ranged from 2.1 to 2.8 while file_exists ranged from 4.0 to 5.8 so stream_resolve tended to have much more consistent performance over time as well.
stream_resolve_include_path
(PHP 5 >= 5.3.2)
stream_resolve_include_path — Resolve filename against the include path
Description
string stream_resolve_include_path
( string
$filename
)
Resolve filename against the include path according to the same rules as fopen()/include.
Parameters
-
filename -
The filename to resolve.
Return Values
Returns a string containing the resolved absolute filename, or FALSE on failure.
Examples
Example #1 stream_resolve_include_path() example
Basic usage example.
<?php
var_dump(stream_resolve_include_path("test.php"));
?>
The above example will output something similar to:
string(22) "/var/www/html/test.php"
fsgale at live dot ca
25-Apr-2012 08:25
kontakt at victorjonsson dot se
25-Apr-2012 03:13
This seems to be a great alternative to file_exists.
if( file_exists(__DIR__.'/som-file.php') )
Goes way slower than:
if( stream_resolve_inlcude_path(__DIR__.'/som-file.php') !== false)
sebastian dot krebs at kingcrunch dot de
16-Feb-2011 09:03
It really behaves like `include` and will only resolve the filename against the include-path, if the path is relative. It makes not much sense to resolve already absolute pathnames anyway.
