Jump to content

Recommended Posts

Posted

Hiya,

 

does anybody know a way to list the contents of a web directory folder.

 

for example i have a folder which has 4 pdf files in, with one more added each month. for this i would like a webpage front end which lets you choose which month you would like to view.

 

e.g. i have october 09.pdf november 09.pdf december 09.pdf january 10.pdf and i would like to have a webpage that lists the contents of the folder automatically so i don't need to add each page in manually

 

Toby

Posted

Can you use PHP scripts on the web server? If so, the following should get you there:

 

function dirList ($directory) 
{

   // create an array to hold directory list
   $results = array();

   // create a handler for the directory
   $handler = opendir($directory);

   // keep going until all files in directory have been read
   while ($file = readdir($handler)) {

       // if $file isn't this directory or its parent, 
       // add it to the results array
       if ($file != '.' && $file != '..')
           $results[] = $file;
   }

   // tidy up: close the handler
   closedir($handler);

   // done!
   return $results;
}

//Call the function above
$results = dirList('PATH TO DIR');

//Print results
foreach($results as $item){
   echo "$item
";
}

Source: Jon Haworth - PHP: List files in a directory (tweaked to give you an example)

 

If you want to add a link to each file simply change the foreach to:

 

foreach($results as $item){
   echo '' . $item . '
';

 

All this code should work, but I haven't tested it.

Posted
Depends how much you want to customise it. For a simple list, just the name of the directory typed into the address bar will do it

 

Most web servers have directory listing disabled, and it's good practice to do this. Therefore in most setups this won't work and will instead produce a "Directory Listing not allowed" error.

Posted
If it's a public facing site you really don't want that script to work, to be honest. Even if it's for an intranet probably not

 

Why not?

 

regards

 

Simon

Posted
If it's a public facing site you really don't want that script to work, to be honest. Even if it's for an intranet probably not

 

I can't see anything which could cause an issue. It would be safer than turning on directory listing (as that could accidentally lead to directories you don't want to be exposed, being exposed).

Posted
If it's a public facing site you really don't want that script to work, to be honest. Even if it's for an intranet probably not

 

There's nothing the matter with that script. The only security hole is that it shows files in a directory (and in the latter one provides a link to the file), but that is what the OP wants to do.

  • 2 months later...
Posted

Sorry to drag this up - have just got round to fully testing my website!

 

I have the following code - Edited from the above I have succesfully made it list the contents of the folder (/documents/newsletters but when you click the hyperlink it is looking in the root of the website not the folder that i am after - which as you can imagine is rather annoying! it might just be me as its late on a bank holiday but i can't see where im going wrong!

 
$myDirectory = opendir("./documents/newsletters/");


while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}


closedir($myDirectory);


$indexCount	= count($dirArray);



sort($dirArray);


print("</pre><table border="1" cellpadding="5" cellspacing="0" class="whitelinks">\n");
print("Issue\n");

for($index=0; $index < $indexCount; $index++) {
       if (substr("$dirArray[$index]", 0, 1) != "."){ 
	print("$dirArray[$index]");
	print("\n");
}
}
print("</table>\n");<br

 

Any help appreciated! now time for sleep!

 

Toby

Guest leeneilson
Posted

You should just need to put the folder "documents/newsletters/" in the link and it will work.

 

So:

 

print("$dirArray[$index]");

 

Would become:

 

print("$dirArray[$index]");

 

I have rewritten the code below to use the PHP SPL library to access the files if your interested.

 

$directoryPath = 'documents/newsletters';

$linksArray = array();
$filesArray = new DirectoryIterator($directoryPath);

foreach ($filesArray as $file) {
if($file->isDot()) {
	continue;
}

$linksArray[] = $file->getFilename();
}

if ($linksCount = count($linksArray)) {
echo '</pre><table border="1" cellpadding="5" cellspacing="0" class="whitelinks">Issue';

foreach ($linksArray as $link) {
	echo '' . $link . '';
}

echo '</table>';<br

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...