Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4327696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:34:30+00:00 2026-05-21T09:34:30+00:00

I am using a lot of include files to include my 191 files of

  • 0

I am using a lot of include files to include my 191 files of with the collection of functions, classes ect.

A problem for me is I really dislike editing the include files its gets a big mess and sometime i just forget to include something.

Therefore I was wondering, is there a include function for php or own made library that includes all the php files in a folder or even better in its own folder + all its sub-folders.

These things make life much easyer and flexible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:34:31+00:00Added an answer on May 21, 2026 at 9:34 am

    You can use the following function i just created:

    function load_folder($folder, $ext = '.php') {
        foreach (glob("$folder*$ext") as $file) { 
            if (file_exists($file)) {
                require_once($file);
            }
        }
    }
    

    START EDIT

    This is the new version of the same function. Now it allows you to specify folders as folder or folder/ without crashing. Also now it loads all files in all folders and subfolders.

    function load_folder($dir, $ext = '.php') {
        if (substr($dir, -1) != '/') { $dir = "$dir/"; }
        if($dh = opendir($dir)) {
            $files = array();
            $inner_files = array();
            while($file = readdir($dh)) {
                if($file != "." and $file != ".." and $file[0] != '.') {
                    if(is_dir($dir . $file)) {
                        $inner_files = load_folder($dir . $file);
                        if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                    } else {
                        array_push($files, $dir . $file);
                    }
                }
            }
            closedir($dh);
            foreach ($files as $file) {
                if (is_file($file) and file_exists($file)) {
                    $lenght = strlen($ext);
                    if (substr($file, -$lenght) == $ext) { require_once($file); }
                }
            } 
        }
    }
    

    END EDIT

    You can also specify a specific extension if you want to load for example only .txt files in a folder you can execute is like this: load_folder('folder/', '.txt');.
    Remember that someone think that this is somehow insecure. Before using this function inside a business site, look for more opinion about the topic.
    Notice also that if some of your files are regarding classes you could use the __autoload() PHP native function to let PHP call the class where it is really needed (lazy loading).

    References:

    • Autoloading classes
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am using a lot of temporary files in java and my problem
I have an odd problem...I'm using a documentation generator which generates a lot of
I am using OpenNI on OS X, and for some reason its .h files
I have quite a lot of PHP view files, which I used to include
Recently I've been using lot of assembly language in *NIX operating systems. I was
Normally (ie. not concurrently), putAll() cannot be more efficient than using lot's of calls
I am using a lot of HTTP Requests in an application that I am
I am using a lot of caching and buffering of API calls in my
Qwt seems to be using a lot of magic numbers. Could anyone please explain
I have a facebook app using a lot of Javascript that doesn't work correctly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.