arrays - How to sort a list of files & directories so directories are listed first in PHP -
How can I sort a list of files & amp; Directory directories are listed in PHP first ??? I try natsort, but it's just alphabetical order like my source:
$ dirFiles = array (); $ Path = "./folder_path"; // Opens the images folder if ($ handle = opendir ($ path)) while {wrong! == ($ file = readdir ($ handle))} {if ($ file! = "." & Amp; $ file ! = ".." & $ file! = "Index.php" & amp; $ file! = "thumbnail" & amp; $ file! = ".DS_Store") {$ isdir = $ path $ file; If (is_dir ($ isdir)) {$ dirFiles [] = $ file; } If (! Is_dir ($ isdir)) {$ dirFiles [] = $ file; }}} Closed ($ handle); } Natsort ($ dirFiles); Listing files and directories
foreach ($ dirFiles $ file = & gt; $ value) {$ name = basename ($ value); $ Name echo; } You can create a multi-dimensional array:
if_dir ($ isdir)) {$ dirFiles ['dir'] [] = $ file; } If ($! Isdir) {$ dirFiles ['file'] [] = $ file; } To list files and directories:
ksort ($ dirFiles); Foreign currency ($ dirFiles as dollar files) {foreach (files $ $ as file) {$ name = basename ($ file); $ Name echo; }}
Comments
Post a Comment