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
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

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.