Home » Php » php – Require all files in a folder

php – Require all files in a folder

Posted by: admin April 23, 2020 Leave a comment

Questions:

Is there an easy way to require all files in a folder?

How to&Answers:

No short way of doing it, you’ll need to implement it in PHP. Something like this should suffice:

foreach (scandir(dirname(__FILE__)) as $filename) {
    $path = dirname(__FILE__) . '/' . $filename;
    if (is_file($path)) {
        require $path;
    }
}

Answer:

Probably only by doing something like this:

$files = glob($dir . '/*.php');

foreach ($files as $file) {
    require($file);   
}

It might be more efficient to use opendir() and readdir() than glob().

Answer:

There is no easy way, as in Apache, where you can just Include /path/to/dir, and all the files get included.

A possible way is to use the RecursiveDirectoryIterator from the SPL:

function includeDir($path) {
    $dir      = new RecursiveDirectoryIterator($path);
    $iterator = new RecursiveIteratorIterator($dir);
    foreach ($iterator as $file) {
        $fname = $file->getFilename();
        if (preg_match('%\.php$%', $fname)) {
            include($file->getPathname());
        }
    }
}

This will pull all the .php ending files from $path, no matter how deep they are in the structure.

Answer:

Simple:

foreach(glob("path/to/my/dir/*.php") as $file){
    require $file;
}

Answer:

Use a foreach Loop.

foreach (glob("classes/*") as $filename) {
  require $filename;
}

For more details, check out this previously posted question:

Answer:

As require_all() function:

//require all php files from a folder
function require_all ($path) {

    foreach (glob($path.'*.php') as $filename) require_once $filename;

}

Answer:

Solution using opendir, readdir, closedir. This also includes subdirectories.

<?php
function _require_all($path, $depth=0) {
    $dirhandle = @opendir($path);
    if ($dirhandle === false) return;
    while (($file = readdir($dirhandle)) !== false) {
        if ($file !== '.' && $file !== '..') {
            $fullfile = $path . '/' . $file;
            if (is_dir($fullfile)) {
                _require_all($fullfile, $depth+1);
            } else if (strlen($fullfile)>4 && substr($fullfile,-4) == '.php') {
                require $fullfile;
            }
        }
    }

    closedir($dirhandle);
}        

//Call like
_require_all(__DIR__ . '/../vendor/vlucas/phpdotenv/src');

Answer:

recursively all file list and require_once in one directory:

$files = array();

function require_once_dir($dir){

       global $files;

       $item = glob($dir);

       foreach ($item as $filename) {

             if(is_dir($filename)) {

                  require_once_dir($filename.'/'. "*");

             }elseif(is_file($filename)){

                  $files[] = $filename;
             }
        }
}

$recursive_path = "path/to/dir";

require_once_dir($recursive_path. "/*");

for($f = 0; $f < count($files); $f++){

     $file = $files[$f];

     require_once($file);
}

Answer:

My way to require all siblings:

<?php
$files = glob(__DIR__ . '/*.php');
foreach ($files as $file) {
    // prevents including file itself
    if ($file != __FILE__) {
        require($file);
    }
}