Development PHP List Folder Contents - Exclude Filetype

Discussion in 'Software' started by Silver51, 21 Jun 2011.

  1. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Basically I need to list the contents of a folder and output it as a list of hyperlinks while excluding anything with an SWF file type. Actually, it'll only be linking specifically to HTML files.

    I've been using a snippet of code for a while which has worked well in the past, but it looking at it, I can only exclude specific files by name, and not file type.

    I've hit up Google, but to be honest, I'm not sure what I'm looking at. :confused:


    Code:
    <ul>
    <?
    $path = "./files/";
    $narray=array();
    $dir_handle = @opendir($path) or die("Unable to open $path");
    $i=0;
    while($file = readdir($dir_handle))
    {
        if(is_dir($file))
        {
            continue;
        }
        else if($file != '.' && $file != '..')
        {
            //echo "<li><a href='$path/$file'>$file</a></li>";
            $narray[$i]=$file;
            $i++;
        }
    
    }
    rsort($narray);
    
    for($i=0; $i<sizeof($narray); $i++)
    {
    echo "<li><a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a></li>";
    
    }
    
    //closing the directory
    closedir($dir_handle);
    ?>
    </ul>
     
  2. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
  3. badders

    badders Neuken in de Keuken

    Joined:
    4 Dec 2007
    Posts:
    2,635
    Likes Received:
    72
    You could also explode $file by "." then the last item in the split array should be the extension - you can then match that against the excluded filetypes.

    eg:
    Code:
    $filetype = array_pop(explode(".",$file));
    
     
    Silver51 likes this.
  4. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Sorted, thanks. :thumb:
     
  5. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
    It depends on the definition of "file type". If "file type" equals the extension checking, then yeah, of course your solution is the better one. But if someone wants the real file type, then he must use the magic.mime file :).
     

Share This Page