Page 1 of 1

List only some extention (File Listing)

Posted: Sun Nov 09, 2008 2:14 pm
by Peuplarchie
Good day to you all,
I'm working on a pice of code which list all files in a directory and return the list and its content right under the name of the file.

Code: Select all

 
 
<?php
$thelist = "";
 if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
      {
            $thelist .= '<a href="'.$file.'">'.$file.'</a><br/>';
            $contents = file($file);
            $string = implode($contents);
            $thelist .= '<p>'.$string.'</p>';
            
          }
       }
  closedir($handle);
  }
?>
<P>List of files:</p>
<P><?=$thelist?></p>
 
 

I'm looking for a way of choose only 3 or 4 extension only.

How would I dow so ?

Thanks !

Re: List only some extention (File Listing)

Posted: Mon Nov 10, 2008 12:35 am
by novice4eva

Code: Select all

 
$myExt = array('php','dat','txt');
while (false !== ($file = readdir($handle)))
{
$fileChunks = array_reverse(explode(".", $file));
$ext= $fileChunks[0];
if ($file != "." && $file != ".." && in_array($ext,$myExt))
{
 .....
......
}
}
 

Re: List only some extention (File Listing)

Posted: Mon Nov 10, 2008 7:39 am
by mmj
@novice4eva, its a good idea to lower case the extension, just in case.

also I prefer to use string functions, they are usually faster then array functions.

@op, whats the point of putting the file into an array if you just want to implode it afterwards?

Code: Select all

if ($file == '.' || $file == '..' || !in_array(strtolower(substr($file, strrpos($file, '.') + 1)), $exts))
    continue;
$thelist .= '<a href="'.$file.'">'.$file.'</a><br/>';
$string = file_get_contents($file);
$thelist .= '<p>'.$string.'</p>';