glennda Posted January 18, 2010 Posted January 18, 2010 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
LosOjos Posted January 18, 2010 Posted January 18, 2010 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
Hightower Posted January 18, 2010 Posted January 18, 2010 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.
Hightower Posted January 18, 2010 Posted January 18, 2010 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.
glennda Posted January 18, 2010 Author Posted January 18, 2010 i will give the php script a go, i think php scripting in enabled. i will post how i get on Toby
natparnell Posted January 19, 2010 Posted January 19, 2010 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
SimpleSi Posted January 19, 2010 Posted January 19, 2010 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
localzuk Posted January 19, 2010 Posted January 19, 2010 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).
Hightower Posted January 19, 2010 Posted January 19, 2010 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.
glennda Posted April 4, 2010 Author Posted April 4, 2010 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 April 4, 2010 Posted April 4, 2010 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
glennda Posted April 5, 2010 Author Posted April 5, 2010 Cheers lee, i knew it was me just not seeing the obvious! thanks Toby
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now