Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Directory listing sort array by filename and then extension type

Posted on 2004-11-19
10
Medium Priority
?
349 Views
Last Modified: 2012-05-05
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;
}
?>
0
Comment
Question by:rossh1977
  • 4
  • 3
  • 3
10 Comments
 
LVL 2

Assisted Solution

by:b_loco
b_loco earned 1000 total points
ID: 12629949
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
 

Author Comment

by:rossh1977
ID: 12630897
THat's great thanks very much!  Got any ideas for sorting by extension type?

THanks

Ross
0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 1000 total points
ID: 12631003
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rossh1977
ID: 12633263
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
 
LVL 2

Expert Comment

by:b_loco
ID: 12633441
Please post the code around line 77 (from 70 to 85 for example)
0
 

Author Comment

by:rossh1977
ID: 12633476
$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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 12633480
try to replace
for ( $n = 0; $fname[$n] || $ext[$n]; $n++ ) {
with
for ( $n = 0; isset($fname[$n]); $n++ ) {
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 12633487
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
 

Author Comment

by:rossh1977
ID: 12673289
Excellent guys worked a treat, Apologies for not getting back to you for a while, been really busy.

Thanks

Ross
0
 
LVL 2

Expert Comment

by:b_loco
ID: 12677141
;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question