Auto include / require all files under all directories

I would like to automatically have include/require

all .php

files under all directories. For example:

(Directory structure)

-[ classes
    --[ loader (directory)
        ---[ plugin.class.php
    --[ main.class.php
    --[ database.class.php

      

I need a function that automatically downloads all files ending in .php

I've tried all kinds:

$scan = scandir('classes/');
foreach ($scan as $class) {
    if (strpos($class, '.class.php') !== false) {
        include($scan . $class);
    }
}

      

+3


source to share


6 answers


Well, if you want to include all php files with a specific ending even in subdirectories, you need to do a recursive function. Something like that:



function load_classphp($directory) {
    if(is_dir($directory)) {
        $scan = scandir($directory);
        unset($scan[0], $scan[1]); //unset . and ..
        foreach($scan as $file) {
            if(is_dir($directory."/".$file)) {
                load_classphp($directory."/".$file);
            } else {
                if(strpos($file, '.class.php') !== false) {
                    include_once($directory."/".$file);
                }
            }
        }
    }
}

load_classphp('./classes');

      

+5


source


If the php files you want to include are PHP classes then you should use PHP Autoloader

It is not safe to include all php files in all directories automatically. Performance may be affected if you include unnecessary files.

Here's the code that should work (I haven't tested it):



$scan = scandir('classes/');
foreach ($scan as $class) {
  if (strpos($class, '.class.php') !== false) {
    include('classes/' . $class);
  }
}

      

If you want recursive include RecursiveIteratorIterator will help you .

+1


source


This is perhaps the simplest way to recursively search for patterns:

$dir = new RecursiveDirectoryIterator('classes/');
$iter = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($iter, '/^.+\.class\.php$/', RecursiveRegexIterator::GET_MATCH); // an Iterator, not an array

foreach ( $files as $file ) {
  include $file; // $file includes `classes/`
}

      

RecursiveDirectoryIterator

is a cunning beast, so learning all of this is probably impossible. However, there are many concrete examples on interwebs. The trick is the search.

+1


source


$ dir = new RecursiveDirectoryIterator ('change this to your custom root directory');
foreach (new RecursiveIteratorIterator ($ dir) as $ file) {
    if (! is_dir ($ file)) {
        if (fnmatch ('*. php', $ file)) // you can change the file extension to any that you require.
        / * do anything here * /
    }
}
+1


source


function autoload( $path ) {
    $items = glob( $path . DIRECTORY_SEPARATOR . "*" );

    foreach( $items as $item ) {
        $isPhp = pathinfo( $item )["extension"] === "php";

        if ( is_file( $item ) && $isPhp ) {
            require_once $item;
        } elseif ( is_dir( $item ) ) {
            autoload( $item );
        }
    }
}

autoload( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . "classes" );

      

+1


source


The easy way: a simple function using RecursiveDirectoryIterator

instead of glob()

.

function include_dir_r( $dir_path ) {
    $path = realpath( $dir_path );
    $objects = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $path ), \RecursiveIteratorIterator::SELF_FIRST );
    foreach( $objects as $name => $object ) {
        if( $object->getFilename() !== "." && $object->getFilename() !== ".." ) {
            if( !is_dir( $name ) ){
                include_once $name;
            }
        }
    }
}

      

0


source







All Articles