PDA

View Full Version : Directory Listing Control



Scriptbanger
12-22-2006, 06:38 PM
I hope this is fairly simple. I want to create a directory where I can drop stuff that people can download [for free]. I have a great php function that reads the directory and writes out a contents list with live links. One problem, it also writes the single and double dots [. and ..] that lets people move out of the current directory list. This I need to eliminate. I only want the list of actual contents without the change directory links.

[?php
$d = dir (http://php.net/dir)('freebies');
echo "Directory: " . $d->path . "<br>";
while (false !== ($entry = $d->read())) {
echo "<a href='/freebies/".$entry."'>".$entry."</a><br>";
}
$d->close();
?]

Scriptbanger
12-22-2006, 07:01 PM
Got the answer from another coder.

sliver ROCKS! :thumbsup:

[?php
$d = dir (http://php.net/dir)('freebies');
echo "Directory: " . $d->path . "<br>";
while (false !== ($entry = $d->read())) {
if($entry != "." && $entry != "..") { //Don't let . and .. through
echo "<a href='/freebies/".$entry."'>".$entry."</a><br>";
}
}
$d->close();
?]

marek_mar
12-22-2006, 07:44 PM
You actually colored it by yourself?

Tyree
12-22-2006, 08:11 PM
LOL! Now THAT's dedication!

whizard
12-22-2006, 08:55 PM
Wow!

That is a fine solution, but could get really tedious if you wanted to block certain folders along with the '.' and '..'.

If you did this:


<ul>
<?php
$skip_folders = array('.','..','other','private_folder');

$dir = "PATH TO DIR YOU WANT TO READ"
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if(!in_array($file, $skip_folders))
{
print("<li><a href=\"$file\">$file</a></li>");
}
}

}
closedir($dh);
}
?>
</ul>


you would be able to filter out any folders or files which you didn't want to show up simply by adding them to the array.

Dan