PDA

View Full Version : inlude() addon feature - finds the include file no matter where you are


panther21001
05-02-2008, 12:56 PM
I began learning PHP and MySQL a month ago, and the thing I was irritated most about was losing control of my include files (I'm sure I'm not the only person).

I created a very simple class/function so that including files in your site will never be as big of a hassle.

Here is where you create and initialize your class object. (e.g: class_lib.inc.php)

<?
class Includes
{

/**
* include_file - Assists the standard include function
* by automatically finding the correct path the specified
* include file is stored in.
*/
function include_file($incFile)
{
while (!file_exists($incFile) && strlen($incFile) < 100)
{
$incFile = "../" . $incFile;
}

if (!file_exists($incFile))
{
die("Include file " . $incFile . " could not be found.");
}

include ($incFile);
}
}

//Initialize Footer Object
$incFooter = new Includes();
?>


As an example, in your file (e.g: index.php) you would place this line of code:

<? $incFooter->include_file(FOOTER); ?>

In my case, I defined the path to my footer include file in a constants.inc.php document:

<?
//INCLUDE FILE PATHS
define("FOOTER", "required/include/footer.inc.php");
?>


How this works:

The $incFile variable holds the directory (from the 'www' root of your web directory), in my case required/include/footer.inc.php which is where my footer include file is located. The while loop checks through your directory, no matter where the index.inc.php is located, and prepends (if necessary) $incFile with "../" as many times as needed.

Please be aware I have only tested with PHP 5.

Let me know if anything can be changed or made better, as I am technically a 'newbie' coder.

I would like to thank everyone else's contribution to the forums, I have been on here a lot lately and learning very neat tips and tricks. Thanks!

-Philip

RMcLeod
05-02-2008, 02:22 PM
Why not set your inlude path in php.ini to your includes folder. That way any files in this folder can be included from anywhere within your site structure.

panther21001
05-02-2008, 06:17 PM
Why not set your inlude path in php.ini to your includes folder. That way any files in this folder can be included from anywhere within your site structure.

Well their are some reasons not to do that.

1: I don't have control of the php.ini file with my hosting plan, and most people don't.

2: This class can be used with any website or project on your server (if you have multiple domains hosted on the same account)

And their may be some other drawbacks as well. Why edit the php ini? That may be going a little too far and much more of a hassle later on. But thanks for that option, it may suit certain people's needs.

-Philip

Inigoesdr
05-02-2008, 08:17 PM
This could be a security risk if you have a file of the same name in a parent directory(for example multiple installs of the same script). Also, this shouldn't need a class for just one function.

CFMaBiSmAd
05-02-2008, 08:30 PM
A) You can set the include path in your script using either the set_include_path() statement or an ini_set() statement.

B) That's a lot of time consuming code and resources to just replace $_SERVER['DOCUMENT_ROOT'] in front of your path/file.

include($_SERVER['DOCUMENT_ROOT'] . '/' . FOOTER); // note, document root does not include the trailing slash and your defined constant does not include a leading slash, so my example here includes a slash as part of the code. It would be better to include the slash as part of the defined constant.

idalatob
05-04-2008, 04:16 PM
Its a bit of an overkill in my oppinion. But if it makes your life easier...