legohead6 Posted January 23, 2008 Share Posted January 23, 2008 well, i have a code that manages folders and files, it works perfectly, but the folders appear wherever they lie, i would like the folders to appear at the top, so my question, how do i tell the foreach which to display first... heres the snip of code $dir = "members/$user"; $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } $re = array_search('..', $files); $rem = array_search('.', $files); unset($files[$rem], $files[$re]); $totalp = count($files); echo "<div align=center><table><tr><td colspan=2>$uploader</td></tr><tr><td colspan=2>$foldermaker<tr><td></td></tr><tr><td colspan=2><p align=center><u>Current Folders/Files</u></p></td></tr>"; foreach($files as $id => $file){ $folder=explode('.',"$file"); if(!isset($folder[1])){ $doc=base64_encode($file); echo "<tr><td><p align=center><a href=doc.php?f=$doc><u><b>$file</b></u></a></p></td></tr>"; }else{ $file1 = str_replace(" ", "_", "$file"); $file2 = str_replace(".", ".", "$file1"); $doc=base64_encode($file); echo "<tr><td><p align=center><a href=doc.php?f=$doc>$file2</p></td><td>Select Folder</td></tr>"; $p++; } } echo "</table></div>"; Link to comment https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/ Share on other sites More sharing options...
legohead6 Posted January 23, 2008 Author Share Posted January 23, 2008 anyone? or do i have to live with the folders being random? Link to comment https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/#findComment-446675 Share on other sites More sharing options...
taith Posted January 23, 2008 Share Posted January 23, 2008 that'll give you two arrays... one of $dirs... one of $files while (false !== ($filename = readdir($dh))) { if(isdir($filename)) $dirs[] = $filename; else $files[] = $filename; } thenyou just $files=array_merge($dirs,$files); and your array has dirs at the top, files at the bottom Link to comment https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/#findComment-446684 Share on other sites More sharing options...
pdkv2 Posted January 23, 2008 Share Posted January 23, 2008 This will give you the directory list <?php function directoryToArray($directory, $recursive) { $array_items = array(); if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_dir($directory. "/" . $file)) { if($recursive) { $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive)); } $file = $directory . "/" . $file; $array_items[] = preg_replace("/\/\//si", "/", $file); } } } closedir($handle); } return $array_items; } ?> Regards SHarad Link to comment https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/#findComment-446693 Share on other sites More sharing options...
legohead6 Posted January 23, 2008 Author Share Posted January 23, 2008 thanks, i ended up breaking into 2 arrays.. Link to comment https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/#findComment-446718 Share on other sites More sharing options...
taith Posted January 23, 2008 Share Posted January 23, 2008 This will give you the directory list <?php function directoryToArray($directory, $recursive) { $array_items = array(); if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_dir($directory. "/" . $file)) { if($recursive) { $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive)); } $file = $directory . "/" . $file; $array_items[] = preg_replace("/\/\//si", "/", $file); } } } closedir($handle); } return $array_items; } ?> Regards SHarad just as a note... glob() does the same job FAR faster Link to comment https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/#findComment-446935 Share on other sites More sharing options...
pdkv2 Posted January 24, 2008 Share Posted January 24, 2008 Thanks taith ! Link to comment https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/#findComment-447522 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.