Forum Moderators: coopster

Message Too Old, No Replies

PHP include files

         

dave1236

3:02 am on Jul 29, 2006 (gmt 0)

10+ Year Member



I am looking for a better way to organize my include files. Currently, I have them in a folder, but in the pages that use them, I end up doing include statements for each file.

The question is, is there a way to reference the folder name, which automatically will pull all the files in that folder. That way, I can simply include the folder, this will simplify my changes!

thanks

coopster

2:06 pm on Jul 29, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could create one include file that would be the "all-inclusive" include ... say that ten times fast. For example, you have a directory called "includes" and within that directory you have "include1.php", "include2.php", etc. as well as some subdirectories and more includes. You could create one main configuration include that includes all the others:

main_include.php

<?php 
include 'include1.php';
include 'include2.php';
include 'include3.php';
include 'subdir1/include4.php';
include 'subdir1/include5.php';
include 'subdir1/anothersubdir/include6.php';
?>

Also, have a look at the PHP configuration directive auto_prepend_file [php.net].

supermoi

9:32 pm on Jul 29, 2006 (gmt 0)

10+ Year Member



You could try something like this (untested):

$path = 'includedirectory/';
$handle = opendir($path);
while (false!== ($file = readdir($handle))) {
include($path.$file);
}
closedir($handle);

dave1236

3:53 am on Aug 1, 2006 (gmt 0)

10+ Year Member



Thanks for the comments. In looking at the various solutions, Coopster's seems to be more to my level of understanding. That said, it would seem that Coopster is telling me that my desire to simply create a subdirectory, call it 'subdir', (with a bunch of .inc in the directory)and simply do something like this:

<?php
include("subdir")
?>

Thanks for your help

coopster

11:29 am on Aug 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, not a subdirectory, a file. You can't include() [php.net] a directory, only a file. Best read that again ;)

dave1236

2:27 am on Aug 2, 2006 (gmt 0)

10+ Year Member



thanks...i think that is what i was afraid of. i was hoping to simply have a directory that would catch all my files in that directory, and i would only have one place to update. thanks for your help!

coopster

2:48 am on Aug 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The two examples shown here will do that for you though ... have a closer look at how it works. The first one shows a hard-coded example. The second shows a dynamic example that will read through the directory and automatically add any include files from that directory by looping through the files within that directory and include them. Give it a shot, see if it works for you!

dave1236

3:31 am on Aug 4, 2006 (gmt 0)

10+ Year Member



coopster...

Thanks for the update. I am beginning to understand the code examples. That is why I ask these questions...I am not a programmer by trade, but have been doing some. The ability to ask these questions of 'those in the know' certainly helps me.

coopster

2:52 pm on Aug 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome.
That's why this forum exists ;)