Aidan's PHP Repository

A repository for PHP functions and classes ...

function.file_exists_incpath.php

Check if a file exists in the include path

  • Version: 1.2.1
  • Author: Aidan Lister <aidan@php.net>
  • Link: http://aidanlister.com/repos/v/function.file_exists_incpath.php
  • Return: mixed The full path if file exists, FALSE if it does not
  • Views: 22159
  • Downloads: 1878

Source

Download this script <?php
/**
 * Check if a file exists in the include path
 *
 * @version     1.2.1
 * @author      Aidan Lister <aidan@php.net>
 * @link        http://aidanlister.com/repos/v/function.file_exists_incpath.php
 * @param       string     $file       Name of the file to look for
 * @return      mixed      The full path if file exists, FALSE if it does not
 */
function file_exists_incpath ($file)
{
    $paths = explode(PATH_SEPARATOR, get_include_path());
 
    foreach ($paths as $path) {
        // Formulate the absolute path
        $fullpath = $path . DIRECTORY_SEPARATOR . $file;
 
        // Check it
        if (file_exists($fullpath)) {
            return $fullpath;
        }
    }
 
    return false;
}
 
?>

Comments

July 31st, 2005
this code works super! thx @ u *gg*
May 23rd, 2005
Excellent code, thanks!
May 9th, 2005
didn't use the script but used the DIRECTORY_SEPARATOR, v.useful as i was being silly when building on a windows box then running on a Linix box. file_exists() was giving me a duff answer coz of the \ / thing. Thx :)
March 22nd, 2005
Thx for usefull code! :-)
February 11th, 2005
Very nice, but only works with php >= 4.3.0, because of the get_include_path() function... Use ini_get('include_path') instead
February 4th, 2005
Very useful function - although I've one suggestion. Instead of returning true - return $fullpath. This will evaluate to true - but also give the caller something to use. Regards, Dasher [Editor's Note: Yes, good point. I've made this change.]
February 2nd, 2005
Thanks for this function,
January 15th, 2005
nice code, very usefull