Directory listing sort array by filename and then extension type

Hi Could you someone let me know if their is any problems that i may come across with this.  I put the switch function in a seperate file as i'll maybe need to add more at a later date.  The only thing i can seem to do is sort the array by extension as well as fielname\dir, if you've any ideas, be a great help.

Thanks

Ross

<html>

<head>

<title></title>

<link rel="stylesheet" href="style.css" type="text/css" media="screen"/>

</head>

<body>

<table border="0" cellpadding="2" cellspacing="0" id="content">

<?php
     $the_array = Array();
     $handle = opendir('.');

     while (false !== ($file = readdir($handle)))
     {
             if ($file != "." && $file != "..")
          {  
                 $the_array[] = $file;
             }
     }
     closedir($handle);
     
     

     foreach ($the_array as $file)
     {
          if ($file != "index.php")
          {
               $filesize = filesize ($file);
               $filesizekb = round($filesize / 1000);
               if ($filesizekb < "1000") {
               $filesizet = $filesizekb;
               $filesizet .= " KB";
          }
          else
          {
               $filesizet = $filesizekb / 1024;
               $filesizet = substr($filesizet, 0, 3);
               $filesizet .= " MB";
          }
         
         
               
               
          $ext  = substr($file, -1*(strlen($file)-strrpos($file,"."))+1);
               
               require("icon.php");
               sort($the_array);
               
               if (!is_dir($file))
               {
                    echo "<tr><td><img src=\"image//".$image_src."\"border='0'></td><td><a href=\"".$file."\" title='".$file."'>".$file."&nbsp;[$filesizet]</a></td></tr>\n";
               }
          }

          if (is_dir($file))
          {
               $date = filemtime($file);
               echo "<tr><td><img src=\"image//dirIcon.gif\"border='0'></td><td><a href=\"".$file."\" title='".$file."'>".$file."</a></tr>";
          }
     }


?>

</body>

</html>

***********
icon.php
***********
<?php
switch($ext)
{
     case "doc":
     case "rtf":
          $image_src = "wordIcon.gif";
          break;

     case "xls":
          $image_src = "xlsIcon.gif";
          break;
         
     case "ppt":
          $image_src = "pptIcon.gif";
          break;
         
     case "pdf":
          $image_src = "pdfIcon.gif";
          break;

     case "zip":
     case "rar":
          $image_src = "zipIcon.gif";
          break;

     case "txt":
          $image_src = "txtIcon.gif";
          break;

     default:
          $image_src = "unknown.gif";
          break;
}
?>
rossh1977Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b_locoCommented:
I think it would work, but I wouldn't do it like that. It can get messy.

As an alternative, I normally have a file called functions.php which contains the most general functions of the project and do as such:

functions.php:
<?php

function getIcon($ext){
    switch($ext)
{
     case "doc":
     case "rtf":
          $image_src = "wordIcon.gif";
          break;

     case "xls":
          $image_src = "xlsIcon.gif";
          break;
         
     case "ppt":
          $image_src = "pptIcon.gif";
          break;
         
     case "pdf":
          $image_src = "pdfIcon.gif";
          break;

     case "zip":
     case "rar":
          $image_src = "zipIcon.gif";
          break;

     case "txt":
          $image_src = "txtIcon.gif";
          break;

     default:
          $image_src = "unknown.gif";
          break;
}
    return $image_src;
}

?>

Then, on the main file:
<?php

include("functions.php");

//code here

$image_src = getIcon($ext);

?>

It's better like this because you can pass the variable to the function, not having to use $ext in every situation
0
rossh1977Author Commented:
THat's great thanks very much!  Got any ideas for sorting by extension type?

THanks

Ross
0
ZhaawZSoftware DeveloperCommented:
See sample below.
I created also an array called $size, which contains information about filesizes. You may create also other arrays that contain information about file, but it's not necessary if you don't need it in sorting - in this case just get needed information when displaying results. However, if you have created some array with info, don't forget to notice it in multisort function.

<pre>
<?php
$dir = opendir('.');
while ( ( $file = readdir($dir) ) !== false ) {
  if ( is_dir($file) ) { continue; }
  if ( !strpos($file, '.') ) {
    $ext  [] = '';
    $fname[] = $file;
  } else {
    $ext  [] = substr(strrchr($file, '.'), 1);
    $fname[] = substr($file, 0, strrpos($file, '.'));
  }
  # some additional information about files, if needed
  $size[] = filesize($file);
}
closedir($dir);
# to sort arrays, use array_multisort() function.
# write all arrays as parameters. you should notice the order of arrays.
# to sort by extension, then by filename, then by file size:
array_multisort($ext, $fname, $size);
for ( $n = 0; $fname[$n] || $ext[$n]; $n++ ) {
  echo $fname[$n].($ext[$n]?'.'.$ext[$n]:'')."\n";
}
?>
</pre>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

rossh1977Author Commented:
Hi,

Looks great, but i'm getting this error:-

Notice:  Undefined offset:  1 in c:\inetpub\wwwroot\dir_list\facultyBoard\index.php on line 77

The directory list consits of:-

2000_2001
2001_2002
2002_2003
2004_2005

Thanks

Ross

0
b_locoCommented:
Please post the code around line 77 (from 70 to 85 for example)
0
rossh1977Author Commented:
$dir = opendir('.');
while ( ( $file = readdir($dir) ) !== false ) {
  if ( is_dir($file) ) { continue; }
  if ( !strpos($file, '.') ) {
    $ext  [] = '';
    $fname[] = $file;
  } else {
    $ext  [] = substr(strrchr($file, '.'), 1);
    $fname[] = substr($file, 0, strrpos($file, '.'));
  }
  # some additional information about files, if needed
  $size[] = filesize($file);
}
closedir($dir);
# to sort arrays, use array_multisort() function.
# write all arrays as parameters. you should notice the order of arrays.
# to sort by extension, then by filename, then by file size:
76-array_multisort($ext, $fname, $size);
77-for ( $n = 0; $fname[$n] || $ext[$n]; $n++ ) {
78-  echo $fname[$n].($ext[$n]?'.'.$ext[$n]:'')."\n<br>";
79-}
80-?>

Thanks

Ross
0
ZhaawZSoftware DeveloperCommented:
try to replace
for ( $n = 0; $fname[$n] || $ext[$n]; $n++ ) {
with
for ( $n = 0; isset($fname[$n]); $n++ ) {
0
ZhaawZSoftware DeveloperCommented:
btw, this example will show *only files* and no directories.
To show both files and directories, you'll have to add some code (collect information about directories or smth) instead of 'continue;' on line
  if ( is_dir($file) ) { continue; }
0
rossh1977Author Commented:
Excellent guys worked a treat, Apologies for not getting back to you for a while, been really busy.

Thanks

Ross
0
b_locoCommented:
;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.