Skip to content

Instantly share code, notes, and snippets.

@iksi
Created January 14, 2016 18:13
Show Gist options
  • Save iksi/ad330a760b78eef42c03 to your computer and use it in GitHub Desktop.
Save iksi/ad330a760b78eef42c03 to your computer and use it in GitHub Desktop.
<?php
function compareFolderItems($a, $b) {
if (is_dir($a) && is_dir($b)) {
return strnatcasecmp($a, $b);
}
if (is_file($a) && is_file($b)) {
$compareExtensions = strcasecmp(
pathinfo($a, PATHINFO_EXTENSION),
pathinfo($b, PATHINFO_EXTENSION)
);
if ($compareExtensions === 0) {
return strnatcasecmp($a, $b);
}
return $compareExtensions;
}
if (is_dir($a) && is_file($b)) return -1;
if (is_file($a) && is_dir($b)) return 1;
}
function listFolderItems($folder) {
chdir($folder);
// Get directory items
$items = array_diff(scandir('.'), array('.', '..', '.DS_Store'));
// Folders, type and alphabetically
usort($items, 'compareFolderItems');
return $items;
}
$items = listFolderItems('content');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment