Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> How To Include An Entire Directory In Your Scripts, PHP - Moderate
timdav83
post Mar 24 2006, 01:42 PM
Post #1


Regular Dot
Group Icon
Group: *Premier*
Posts: 137
Joined: 6-October 04
From: Ohio, USA
Member No.: 835





I needed a way to include a bunch of files into my scripts without typing include "file"; each and every time I added something new to the folder so I came up with the below code which may be helpful to some:

CODE
$path_to_includes = "includes/";

$dir_handle = @opendir($path_to_includes) or die("Unable to open $path_to_includes");

$files = array();

while ($file = readdir($dir_handle))
{

$files[] = "$file";
include "$file";

} // end while loop


It's rather small and simple. Basically, we specify a directory to open and then open it for reading with @opendir($path_to_includes); The next step is to create an array $files = array(); then loop through the directory with while($file = readdir($dir_handle)). While looping, we place each file we run across in the array $files[] = "$file"; and then include them in our script with include "$file";

May not be the best way to go sometimes but can be very useful to those who don't want to retype include all the time.
 
+Quote Post  Go to the top of the page
RedDragon
post Mar 24 2006, 02:24 PM
Post #2


insert coin
Group Icon
Group: Main Team
Posts: 5,344
Joined: 24-May 04
From: Maastricht
Member No.: 35





Thx for your addition smile.gif

If I understand this correctly (I'm not a php expert..), it will open all includefiles and echo them all under eachother.

But what would then be the point of having multiple include files instead of one big one?

I might be wrong ofcourse, but I;m trying to get the idea behind it smile.gif


IPB
 
+Quote Post  Go to the top of the page
timdav83
post Mar 24 2006, 03:12 PM
Post #3


Regular Dot
Group Icon
Group: *Premier*
Posts: 137
Joined: 6-October 04
From: Ohio, USA
Member No.: 835





You usually don't make one big class file for every action of your web site. You have one for processing forms, errors, mysql database functions etc. May be easier to just copy and paste this code into your scripts rather then including every single class.
 
+Quote Post  Go to the top of the page
Holic
post Mar 24 2006, 09:28 PM
Post #4


Regular Dot
Group Icon
Group: Member
Posts: 169
Joined: 21-August 04
Member No.: 567





Usually, I just make one "main.inc.php" file that contains my "connect.inc.php", "functions.inc.php", etc.

But I can see some situations where this, too, would be useful.
 
+Quote Post  Go to the top of the page
Memet
post May 2 2006, 12:30 PM
Post #5


New Member
Group Icon
Group: Member
Posts: 23
Joined: 21-April 06
Member No.: 10,871





Thanks for adding this tutorial. I will be sure to try this one out.
 
+Quote Post  Go to the top of the page

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 2nd December 2008 - 01:19 PM
Mortgages | Apply for Credit Card | Mortgage | Ajax Script | Myspace Layouts