Last active
January 6, 2016 08:42
-
-
Save Artem-Schander/38d1cef5bc4399164f17 to your computer and use it in GitHub Desktop.
Get files from folder (date, name, size). + Filter, Sort, Ignore.
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
<?php | |
public function scan_dir($dir, $contains = null, $sort = 'date', $ignored = array('.', '..', '.htaccess')) { | |
$files = array(); | |
$finfo = finfo_open(FILEINFO_MIME_TYPE); | |
foreach (scandir($dir) as $i => $file) { | |
preg_match('/'.$contains.'(\D)/', $file, $matches); | |
if (isset($contains) && is_string($contains) && count($matches) === 0) continue; | |
$files[$i] = array( | |
'date' => filemtime($dir . '/' . $file), | |
'name' => pathinfo($file, PATHINFO_FILENAME), | |
'file' => $file, | |
'path' => $dir, | |
'size' => $this->human_filesize(filesize($dir . '/' . $file)), | |
'mime' => finfo_file($finfo, $dir . '/' . $file), | |
'type' => MIME::group(pathinfo($dir . '/' . $file, PATHINFO_EXTENSION)), | |
); | |
} | |
finfo_close($finfo); | |
usort($files, function($a, $b) use ($sort) { | |
if( is_string($a[$sort]) ) | |
return strcmp(strtolower($a[$sort]), strtolower($b[$sort])); | |
else | |
return $a[$sort] - $b[$sort]; | |
}); | |
return ($files) ? $files : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment