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 — Resuelve el nombre de archivo en la ruta incluida
Descripción
string stream_resolve_include_path
( string
$filename
)
Resuelve filename en la ruta incluida según las mismas reglas que en fopen()/include.
Parámetros
-
filename -
El nombre de fichero a resolver.
Valores devueltos
Devuelve un string que contiene el nombre de fichero absoluto resuelto, o FALSE en caso de error.
Ejemplos
Ejemplo #1 Ejemplo de stream_resolve_include_path()
Ejemplo de uso básico.
<?php
var_dump(stream_resolve_include_path("prueba.php"));
?>
El resultado del ejemplo sería algo similar a:
string(22) "/var/www/html/test.php"
fsgale at live dot ca
25-Apr-2012 07:25
kontakt at victorjonsson dot se
25-Apr-2012 02: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 08: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.
