2

Is it possible to run require_once recursively in one line like this:

<?php
require_once('./OAuth2/*');

Or would you have to go to each file directly and require it?

nkcmr
  • 10,690
  • 25
  • 63
  • 84
  • 1
    You're better off using an autoloader – Mark Baker Apr 22 '13 at 16:45
  • You can use [`glob()`](http://php.net/glob) to get a list of the files that match that pattern in an array, `foreach` over the array and include each element. But you should probably consider using an [autoloader](http://php.net/spl-autoload-register) instead... – DaveRandom Apr 22 '13 at 16:45

2 Answers2

6

cant do it that way. something like this would work:

foreach (glob("./OAuth2/*.php") as $filename)
{
    require_once($filename);
}
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
  • +1 I use this all the time and it works great. I typically do it this way though `foreach (glob("classes/*.php") as $filename) { require_once($filename); }` with the .php there to ensure I dont include any other random files that may be in the directory (maybe like an index.html file) – Ty Bailey Apr 22 '13 at 16:48
0

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);
}
Raju Ram
  • 943
  • 9
  • 15