Created
October 30, 2013 23:11
-
-
Save aswebdev/8c301cb5a545eddbe0b9 to your computer and use it in GitHub Desktop.
Function that Loops files in a directory and puts them into an array
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
// Loop Directory Function | |
function loopDirectory($path,$filetypes='',$stringmatch='') { | |
// initiate the variables | |
$array = array(); | |
$error = ''; | |
// check if $filetypes is set, if so and not an array make an array | |
if((!empty($filetypes)) && (!is_array($filetypes))) { | |
$filetypes = array($filetypes); | |
} | |
// Open the Directory | |
if ($handle = opendir($path)) { | |
// Loop Directory | |
while (false !== ($entry = readdir($handle))) { | |
// Do not include the .. and . file | |
if(($entry != '..') && ($entry != '.')) { | |
$filepath = $path.$entry; // Set the filepath | |
$extension = pathinfo($filepath, PATHINFO_EXTENSION); // Set the file extension | |
$modified = filectime($filepath); | |
$addEntry = false; | |
// Check if our filetype array is set | |
if(is_array($filetypes)) { | |
if(in_array($extension,$filetypes)) { | |
// Check for string match | |
if(!empty($stringmatch)) { | |
if(stristr($entry,$stringmatch)) { | |
$addEntry = true; | |
} | |
} else { | |
$addEntry = true; | |
} | |
} | |
} else { | |
if(!empty($stringmatch)) { | |
if(stristr($entry,$stringmatch)) { | |
$addEntry = true; | |
} | |
} else { | |
$addEntry = true; | |
} | |
} | |
// Flag to add entry to the array | |
if($addEntry) { | |
$array[] = array('file_path' => $filepath, 'filename' => $entry, 'date_modified' => $modified, 'file_extension' => $extension); | |
} | |
} | |
} | |
} else { | |
$error = "Could not open the path ($path)."; // Open Folder Error | |
} | |
if(DEBUG == true) { if($error) { echo $error; exit; } } // Check for Error | |
// Return the result | |
if(count($array) > 0) { | |
return $array; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment