Created
January 19, 2014 20:19
-
-
Save azanebrain/8510489 to your computer and use it in GitHub Desktop.
A PHP Function to get an array of files within a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* getContentsofDir v0.1 | |
* This function gets the contents of a directory. | |
* It returns an array ($contentsArr) of strings for each item in the target directory | |
* $dir : The path of the directory to search in | |
* Attributes: | |
ignoreSystemFiles: Do not include system files in the array | |
Such as: ., .., DS_Store | |
num: How many entries to return. 0 will return all | |
ignore: Ignore this file(s) | |
ignoreType: Ignore this file type(s) | |
*/ | |
function getContentsOfDir($dir, $attributes){ | |
// echo "get contents<br>"; | |
$handle=@opendir($dir); | |
// echo "<b>getContentsOfDir</b>: dir: $dir <br> handle: '$handle' <br>"; | |
if($handle){ | |
$contentsArr=array(); //this array holds the names of all the files in the target dir | |
$index=1; //How many times the loop has run | |
while ($entry = readdir($handle)) { //While there are files in the target dir | |
if($attributes['ignoreSystemFiles'] && $entry=="." | |
|| $attributes['ignoreSystemFiles'] && $entry==".." | |
|| $attributes['ignoreSystemFiles'] && $entry==".DS_Store" | |
|| $attributes['ignoreSystemFiles'] && $entry=="Thumbs.db"){ | |
$index--; //Subtract from index so the right number of entries is returned | |
}//ignoreSystemFiles | |
else if ( substr($entry, -3) != "php" ) { | |
//not a php file. Do nothing. | |
} | |
else if($attributes['ignore'] | |
&& in_array ( $entry , $attributes['ignore'] ) ) { | |
//do nothing | |
} | |
else{ | |
array_push($contentsArr, $entry); //Add the files to the array | |
}//push entry | |
$index++; | |
}//while | |
@closedir($handle); | |
return $contentsArr; | |
// echo "count: ".count($contentsArr)."\n"; | |
}//if handle | |
@closedir($handle); | |
// echo "RETURNING:nothing <br>"; | |
return; | |
}//getContentsOfDir | |
//Set the contents of the target dir into an array and returns that array. If the directory cannot be found, or if no entries were in the directory, it returns null | |
function setFunctionsArr($functionsDir){ | |
//Create an array with each value of the dir | |
$functionsArr = getContentsOfDir($functionsDir, array( | |
'ignoreSystemFiles' => true | |
, 'ignore' => array( | |
'setEnvironment.php' | |
, 'enqueue_scripts.php' | |
, 'enqueue_styles.php' | |
, 'functions-customize.php' | |
, 'watson_setup.php' | |
, 'watson_custom.php' | |
) | |
)); | |
if(@$functionsArr){ | |
return $functionsArr; | |
}//if | |
else{ | |
return; | |
} | |
}//setFunctionsArr | |
//Includes all the entries of an array | |
function includeArrEntries($myArr){ | |
if(@isset($myArr)){ | |
foreach($myArr as $entry){ | |
include('functions/'.$entry); | |
}//foreach | |
}//if | |
}//includeArrEntries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment