note 99547 deleted from function.file-exists by danbrown

From: Date: Tue, 24 Aug 2010 03:42:23 +0000
Subject: note 99547 deleted from function.file-exists by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171962@lists.php.net to get a copy of this message
Note Submitter: David S. ---- @plsnhat That's a nice function I tend to use three functions for getting files on the include path this might be overKill but is works for me. I wished they had a file_exists($file, $on_include_path = false); here are the three function I use, they are located in a file that I only include once <?php function prep_include_path() { static $path; $include_path = ini_get('include_path'); if ($include_path !== $path) { $include_path = str_replace('\\', '/', $include_path); $include_path = explode(PATH_SEPARATOR, $include_path); $key = array_keys($include_path); $size = count($key); for ($i = 0; $i < $size; $i++) { if (substr($include_path[$key[$i]], -1) !== '/') { $include_path[$key[$i]] .= '/'; } } $path = implode(PATH_SEPARATOR, $include_path); ini_set('include_path', $path); } return; } prep_include_path(); function get_include_array() { static $path_array; static $path_string; $include_path = ini_get('include_path'); if ($include_path !== $path_string) { $path_array = explode(PATH_SEPARATOR, $include_path); $path_string = $include_path; } return $path_array; } function on_include_path($file = null) { if ($file === null) { return false; } $include_path = get_include_array(); $key = array_keys($include_path); $size = count($key); for ($i = 0; $i < $size; $i++) { $test = $include_path[$key[$i]] . $file; if(file_exists($test)){ if($include_path[$key[$i]] === './'){ $test = realpath($test); if(is_dir($test)){ $test .= '/'; } } return $test; } } return false; } ?> It's not the prettiest and am sure it could be made more elegant on_include_path I would add a backslash on the end of $file parameter for a directory. personally I think every entry on the include path should have a closing DIRECTORY_SEPARATOR

« previous php.notes (#171962) next »