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