Jump to content

display folders on top of files


matthew9090

Recommended Posts

how would you display folders on top of files

 

part of my code:

while (($file = readdir($dir)) !== false)
  {
    if (!strstr($file, ".")) //get folders
         {
            echo "<a href='index.php?dir=$dir/$file'>" . $file . "</a><br />";
         }
      if ($file!="." && $file!=".." && strstr($file, "."))
      {
         echo $file . "<br />";
      }

Link to comment
https://forums.phpfreaks.com/topic/239057-display-folders-on-top-of-files/
Share on other sites

but now if i do

<?php
while (($files = readdir($dir2)) !== false)
  {
   if (is_dir($files))
   {
      if ($files != "." && $files  != "..")
      {
         echo $files . "<br />";
      }
   }
   elseif (is_file($files))
   {
      echo $files . "<br />";
   }
  }
?>

 

then it only comes up with 1 directory and 1 file, but last time it came up with everything

You need to add each directory/file the code finds to a seperate array and then iterate over the directory/files array to display the directory/files in the order you want. example untested code

$directories = array();
$files = array();

while (($file = readdir($dir2)) !== false)
{
    if ($file != "." && $file  != "..")
    {
        if (is_dir($file))
        {
            $directories[] = $file;
        }
        elseif (is_file($file))
        {
            $files[] = $file;
        }
    }
}

echo '<pre>' . print_r($directories, true) . '</pre>';
echo '<pre>' . print_r($files, true) . '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.