note 93253 added to function.file-exists
| From: | GregMagarshak at osu1 dot php dot net | Date: | Sun, 30 Aug 2009 21:19:43 +0000 |
| Subject: | note 93253 added to function.file-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160065@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 24.190.26.95
----
Manual Page -- http://www.php.net/manual/en/function.file-exists.php
Edit -- https://master.php.net/note/edit/93253
Del: integrated -- https://master.php.net/note/delete/93253/integrated
Del: useless -- https://master.php.net/note/delete/93253/useless
Del: bad code -- https://master.php.net/note/delete/93253/bad+code
Del: spam -- https://master.php.net/note/delete/93253/spam
Del: non-english -- https://master.php.net/note/delete/93253/non-english
Del: in docs -- https://master.php.net/note/delete/93253/in+docs
Del: other reasons-- https://master.php.net/note/delete/93253
Reject -- https://master.php.net/note/reject/93253
Search -- https://master.php.net/manage/user-notes.php