note 99547 added to function.file-exists

From: Date: Tue, 24 Aug 2010 02:43:53 +0000
Subject: note 99547 added to function.file-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-171959@lists.php.net to get a copy of this message
@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 ---- Server IP: 192.41.42.26 Probable Submitter: 24.38.176.64 ---- Manual Page -- http://www.php.net/manual/en/function.file-exists.php Edit -- https://master.php.net/note/edit/99547 Del: integrated -- https://master.php.net/note/delete/99547/integrated Del: useless -- https://master.php.net/note/delete/99547/useless Del: bad code -- https://master.php.net/note/delete/99547/bad+code Del: spam -- https://master.php.net/note/delete/99547/spam Del: non-english -- https://master.php.net/note/delete/99547/non-english Del: in docs -- https://master.php.net/note/delete/99547/in+docs Del: other reasons-- https://master.php.net/note/delete/99547 Reject -- https://master.php.net/note/reject/99547 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171959) next »