note 86804 modified in function.file-exists by danbrown

From: Date: Tue, 04 Nov 2008 17:00:37 +0000
Subject: note 86804 modified in function.file-exists by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146127@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): <?php 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!"); } ?> --was-- 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!"); } http://php.net/manual/en/function.file-exists.php

« previous php.notes (#146127) next »