note 93253 deleted from function.file-exists by joey
| From: | joey@php.net | Date: | Fri, 18 Nov 2011 02:07:50 +0000 |
| Subject: | note 93253 deleted from function.file-exists by joey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184554@lists.php.net to get a copy of this message | ||
Note Submitter: Greg Magarshak
----
Be aware: If you pass a relative path to file_exists, it will return false unless the path happens
to be relative to the "current PHP dir" (see chdir() ).
Here is a function I wrote to get the real path of a relative filename. You can use this in place of
file_exists.
<?php
/**
* Check if a file exists in the include path
* And if it does, return the absolute path.
* @param string $filename
* Name of the file to look for
* @return string|false
* The absolute path if file exists, false if it does not
*/
function realPath ($filename)
{
// Check for absolute path
if (realpath($filename) == $filename) {
return $filename;
}
// Otherwise, treat as relative path
$paths = explode(PS, get_include_path());
foreach ($paths as $path) {
if (substr($path, -1) == DS) {
$fullpath = $path.$filename;
} else {
$fullpath = $path.DS.$filename;
}
if (file_exists($fullpath)) {
return $fullpath;
}
}
return false;
}
?>