Listing files but hiding directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mbgritten
Forum Newbie
Posts: 7
Joined: Tue Jul 28, 2009 2:18 am

Listing files but hiding directories

Post by mbgritten »

Hello,

I'm trying to get a script working which will write a list of links to files within any directory the script is dropped into, but following these criteria:

a) Certain file extensions should be hidden (e.g. no *.gif files showing)
b) Some files should be filtered using DOS */? criteria (e.g. config*.*)
c) No directories should show
d) The file listing should hide the file extension (e.g. myfile.php should show as myfile)
e) The file listings should be capitalised

I've got this far:

Code: Select all

<?php 
// function to strip off the file extension 
function strip_ext($name) { 
    $ext = strrchr($name, '.'); 
    if($ext !== false) { 
    $name = substr($name, 0, -strlen($ext)); 
} 
return $name; 
} 
// function to remove the hyphen or underscore from filename. 
 
function removeHyphen($filename) { 
    $target = $filename; 
    $patterns[0] = '/-/'; 
    $patterns[1] = '/_/'; 
    $replacements[0] = ' '; 
    $replacements[1] = ' '; 
    $filename = preg_replace($patterns, $replacements, $target); 
    return $filename; 
} 
// function to capatalize the first character of each word. Must be called after 
// the removal of the hyphen or underscore 
 
function capFirstWord($word) { 
    $cap = $word; 
    $cap = explode(' ', $cap); 
    foreach($cap as $key => $value) { 
    $cap[$key] = ucfirst($cap[$key]); 
} 
$word = implode(' ', $cap); 
return $word; 
} 
// Formats the file. This is the main function that calls all functions explained above. 
function formatFile($name) { 
    $name = strip_ext($name); 
    $name = removeHyphen($name); 
    $name = capFirstWord($name); 
    return $name; 
} 
 
// Sets up the directory you would like to use as the "reading" directory. 
$mydir = dir('./'); 
 
// setup a blacklist of extension to ignore: 
$extension_blacklist = array("gif", "js", "css", "config*.*"); 
 
while (($file = $mydir->read()) !== false) 
{ 
    // get information about the file: 
    $file_info = pathinfo($file); 
 
    // Security - remove "." and ".." files (directories) 
    if ($file != "." && $file != ".." && $file != "printpage.php" && !in_array($file_info['extension'], $extension_blacklist)) 
    { 
        // Output. This is what is actually outputed by the script and that shows 
        // up on the screen (browser). 
        echo "<li><a href='./$file'>".formatFile($file)."</a></li>";
    } 
} 
$mydir->close();
?>
But I'm struggling with:

a) Hiding the directories
b) Filtering out certain files using the DOS wildcards

:banghead:

Can anyone help in either showing me what to do or pointing me in the right direction?

Thank you in advance!!

Mark
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Listing files but hiding directories

Post by Darhazer »

a) if (is_dir($file)) continue
b) use preg_match
mbgritten
Forum Newbie
Posts: 7
Joined: Tue Jul 28, 2009 2:18 am

Re: Listing files but hiding directories

Post by mbgritten »

Hello,

Thanks for the quick reply. Can you point me where I should include this in the script? Every time I've tried to do it, either I come up with an error or it ignores the extra code entirely.

Mark
mbgritten
Forum Newbie
Posts: 7
Joined: Tue Jul 28, 2009 2:18 am

Re: Listing files but hiding directories

Post by mbgritten »

Thanks for letting me know.

Mark
Post Reply