Adam W
Adam W

Reputation: 337

Auto include/require all files under all directories

I would like to automatically 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 loads all files that end in .php

I have tried all-sorts:

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

Upvotes: 4

Views: 16434

Answers (8)

Arman Sargsyan
Arman Sargsyan

Reputation: 11

This case work for me.

private static function includeFolderRecursivly( $directory ) {
    $platforms = scandir( $directory );
    $platforms = array_diff( $platforms, array( '.', '..' ) );
    foreach ( $platforms as $item ) {
        if ( isset ( pathinfo( $item )['extension'] ) && pathinfo( $item )['extension'] == 'php') {
            require_once $directory . pathinfo( $item )['basename'];
        } else {
            self::includeFolderRecursivly( $directory . pathinfo( $item )['basename'].'/' );
        }
    }
}

Its including only files with extension '.php'

Upvotes: 0

Abdulbasit
Abdulbasit

Reputation: 780

One that I use is

function fileLoader($dir) {
    $files = scandir($dir);
    foreach ($files as $file) {
        if ($file == '.' || $file == '..') {
            continue;
        }
        $path = $dir . '/' . $file;
        if (is_dir($path)) {
            __DIR__.$path;
        } else {
            require_once $path;
        }
    }
}

# calling the function
fileLoader('mydirectory')

Upvotes: 0

J.BizMai
J.BizMai

Reputation: 2777

An 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;
            }
        }
    }
}

Upvotes: 0

Nathan Arthur
Nathan Arthur

Reputation: 9096

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" );

Upvotes: 1

Mike Musni
Mike Musni

Reputation: 171

$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 */
    }
}

Upvotes: 1

Rudie
Rudie

Reputation: 53821

This is probably the simplest way to recursively find patterned files:

$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 tricky beast, so learning all about it is probably not doable. The interwebs has many specific examples though. The trick is to search for it.

Upvotes: 2

ggirtsou
ggirtsou

Reputation: 2048

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

It's not a safe practice to include all php files in all directories automatically. Performance might be degraded if you're including unnecessary files.

Here's the code that should work (I have not 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.

Upvotes: 1

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

Well, if you want to include all php files with a certain ending even in subdirectories, you have to make a recursive function. Something like this:

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');

Upvotes: 8

Related Questions