note 86804 added to function.file-exists
| From: | kevin at cuznersoft dot com | Date: | Tue, 04 Nov 2008 16:32:58 +0000 |
| Subject: | note 86804 added to function.file-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146125@lists.php.net to get a copy of this message | ||
My way of making sure files exist before including them is as follows (example: including a class
file in an autoloader):
function __autoload($name)
{
$path = explode(":", ini_get('include_path')); //get all the possible paths to
the file (preloaded with the file structure of the project)
foreach($path as $tryThis)
{
//try each possible iteration of the file name and use the first one that comes up
// name.class.php first
$exists = file_exists($tryThis . '/' . $name . '.class.php');
if ($exists)
{
include_once($name . '.class.php');
return;
}
//ok that didn't work, try the other way around
$exists = file_exists($tryThis . '/' . 'class.' . $name . '.php');
if ($exists)
{
include_once('class.' . $name . '.php');
return;
}
//neither did that...let's try as an inc.php
$exists = file_exists($tryThis . '/' . $name . '.inc.php');
if ($exists)
{
include_once($name . '.inc.php');
return;
}
}
// can't find it...
die("Class $name could not be found!");
}
----
Server IP: 209.41.74.194
Probable Submitter: 128.187.0.164
----
Manual Page -- http://www.php.net/manual/en/function.file-exists.php
Edit -- https://master.php.net/note/edit/86804
Del: integrated -- https://master.php.net/note/delete/86804/integrated
Del: useless -- https://master.php.net/note/delete/86804/useless
Del: bad code -- https://master.php.net/note/delete/86804/bad+code
Del: spam -- https://master.php.net/note/delete/86804/spam
Del: non-english -- https://master.php.net/note/delete/86804/non-english
Del: in docs -- https://master.php.net/note/delete/86804/in+docs
Del: other reasons-- https://master.php.net/note/delete/86804
Reject -- https://master.php.net/note/reject/86804
Search -- https://master.php.net/manage/user-notes.php