Including directory and all files in it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Including directory and all files in it

Post by kkonline »

I want to include all the files in a particular directory

I know i can write

Code: Select all

include("$_SERVER[DOCUMENT_ROOT]/gb/index.php");
 include("$_SERVER[DOCUMENT_ROOT]/gb/post.php");
and so on for each file.

But is there any way i can add all the files inside gb directory at once?
something like include the directory and all it's files.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Sure. Traverse through the directory.

Code: Select all

foreach(glob('/your/directory/*.php') AS $includeFile)
{
    include '/your/directory/' . $includeFile;
}
EDIT| I wouldn't do that though. There's nothing wrong with including files one at a time. At least you know what you're including, and where they're included.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Also check out scandir.
Post Reply