Skip to content

Instantly share code, notes, and snippets.

@mohsinrasool
Created July 9, 2015 12:32
Show Gist options
  • Save mohsinrasool/4b998c6628bd700f1054 to your computer and use it in GitHub Desktop.
Save mohsinrasool/4b998c6628bd700f1054 to your computer and use it in GitHub Desktop.
Function recursively includes all the files in the specified directory $dir and skips the ones in $skipFiles array
<?php
/**
* It recursively includes all the files in the specified directory $dir and skips the ones in $skipFiles array
* Usage:
* include_files(dirname(__FILE__)."/models", $skipFiles);
*
* @return void
* @author Mohsin Rasool
*
**/
function include_files($dir, $skipFiles = array('.','..')) {
$iterator = new DirectoryIterator($dir);
foreach ($iterator as $fileinfo) {
if(in_array($fileinfo->getFilename(),$skipFiles))
continue;
if( $fileinfo->isFile()) {
require( $fileinfo->getPath().'/'.$fileinfo->getFilename() );
}
else if($fileinfo->isDir()) {
include_files($fileinfo->getPath().'/'.$fileinfo->getFilename());
}
}
}
$skipFiles = array('.','..','model.php','db.php');
// include all the files from models directory
include_files(dirname(__FILE__)."/models", $skipFiles);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment