Skip to content

Instantly share code, notes, and snippets.

@Artem-Schander
Last active January 6, 2016 08:42
Show Gist options
  • Save Artem-Schander/38d1cef5bc4399164f17 to your computer and use it in GitHub Desktop.
Save Artem-Schander/38d1cef5bc4399164f17 to your computer and use it in GitHub Desktop.
Get files from folder (date, name, size). + Filter, Sort, Ignore.
<?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