Jump to content

[SOLVED] Listing files but hiding directories


mbgritten

Recommended Posts

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:

 

<?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

 

:facewall:

 

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

 

Thank you in advance!!

 

Mark

I've got the point at which I've hidden the directories, but now need some help sorting the output alphabetically.

 

Here's where I'm at:

 

<?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 extensions to ignore: 
$extension_blacklist = array("gif", "js", "css"); 
$filter = array("config.php", "printpage.php");

while (($file = $mydir->read()) !== false) 
{ 
    // get information about the file: 
    $file_info = pathinfo($file); 

    // Security - remove "." and ".." files (directories)
    if ($file != "." && $file != ".." && !in_array($file, $filter) && !in_array($file_info['extension'], $extension_blacklist))
    if(!is_dir($file))
    { 
        // 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();
?>

 

Can anyone help me with the directory list sorting?

 

Thanks,

 

Mark

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.