Directory list

Hello everyone. I am re-writing an existing Classic ASP site in PHP. I am doing OK but have come up against something I dont understand:

I have a folder called ‘downloads’
Within it are a number of subfolders. The first is named ‘1NEW’, then then others are A,B,C,etc, to the end of the alphabet.
Each subfolder contains a number of .pdf files, the names of which begin with the letter of the folder in which they are situated.

I want to display a list showing each folder and the files within them. The filenames are displayed as links so that the user can click them to open the .pdf file.

I have managed to do that, with a bit of research and some trial and error, but the code is also displaying some spurious stuff which I dont want and I am mystified as to why it is and how to get rid of it. Here is my code:

$dir = "downloads";
$scan = scandir('downloads');

foreach($scan as $folder) {

if (!is_dir("downloads")) { //do nothing
}else{
if (is_dir("downloads/$folder")) {

//If ($folder == "X") { //do nothing - X is empty folder to cause routine to move to next letter
//}//else{
If ($folder == "1NEW") { //NEW is suffixed with 1 to make it the first folder
$folder = "NEW"; //remove the '1' for display purposes
}

echo "<b>" . $folder . "</b>"; //display the folder name

If ($folder == "NEW") { //replace the '1' as required for file url
$folder = "1NEW";
}
$divider="______________________";
If ($folder == "1NEW") { $divider="___________________"; }
If ($folder == "Hymns") { $divider="__________________"; }
If ($folder == "Christmas") { $divider="_______________"; }
echo "<b>" . $divider . "</b><br><br>"; //display the dividing line

$path = "downloads/" . $folder . "/";

$files = array_diff(scandir($path), array('.', '..'));

foreach($files as $file){

echo "&nbsp;&nbsp;<a href=downloads/" . $folder . "/" . $file . ">" . $file . "</a><br>";
}
echo "<br>"; //space before next folder name
} //end of 'if (is_dir("downloads'
}
}

Here is the url of the page it creates:

Bold’zBrass

You will see that the page shows two spurious lists at the top before we get to the first one which I really want, which is headed ‘NEW’.
The first is a list of folders and files within the ‘downloads’ directory, and the second is a list of all the folders in the ‘BOLDzBRASS’ directory. There are also two lines which start with ‘. ____________’ and ‘… _______________’
Could you show me how to get rid of all that please?

Well, first, welcome to the site! But, please place code inside the code-tags. Or use three backquotes before and after the code ` (no spaces.) It makes it easier for us to read and copy!

Well, several issues with this code. The displayed page is hard to deal with, user-wise. I suggest you place the folder names across the top instead and use that as an index to show the page they selected. Users do not like to scroll any more than they need to. So, it would be easier to show just the items for example “A” and if they select “M” move to that list. This is easy to do just using links.

Scandir() function will show the “.” and “…” folders. You need to skip these when you build your list of files. You should not alter folder names to make them in the correct order. Just code it in a different manner so that the NEW folder is always first. You can create an array of filenames in the selected folder, skipping “.”, “…”, “new”, then display new and the rest. In that way, you do not have to adjust the filenames. The divider code makes no sense since you never set it to anything.???

So, build an array of the current folder first skipping the . , … , and new folder names. Create the array starting with new and it will always be first. Once you have this done, post the new code inside of the three back-ticks and we will help you further…

Hello and thanks for your reply. I appreciate the explanation you have given, which is most helpful, and apologise for not presenting my code in the correct manner.
It was the ‘.’ and ‘…’ folders that confused me. I didn’t realise what they represented, or that scandir includes them in it’s results instead of only including the specified directory.
Now that I know that I have sorted it out and I am now getting the results I want.
This website is not intended for the whole world, it is purely for the use of a small group of musicians and has been operating for about 8 years already. I am only converting it to PHP because I want to migrate the site to another host, and they dont support ASP. I will keep the display format as it is because they are used to it and it would just be unnecessary work to re-hash it.
Thanks again for your help!

Glad we could help. And, yes, do not change something that is working with the users now! Ha!

Nice to hear you solved it. See you in your next post!

PS: I used to use ASP, 20 years ago! I like PHP much better!

Sponsor our Newsletter | Privacy Policy | Terms of Service