Print a directory list, for image gallery

Hi everyone,

First I’ll try to explain as clear as possible to describe what I’m trying to achieve.
Well so I’m writing a CMS for a website, and a gallery is one of the things which should be implented. Ofcourse users should be able to upload images to the gallery. Now the gallery should have different albums and users would be able to either make a new album or upload the images to an already excisting album. (the different albums should be just directories in the main gallery directory) So in the tab for uploading new images there should be an option to select the excisting directories(albums).This list should be auto-updating ofcourse.

therefore I wrote this piece of code

[php]
$path = ‘…/foto/images/’;
$dirs = scandir($path);

foreach ($dirs as $dir) {
if (is_dir($dir) && $dir != ‘.’ && $dir != ‘…’)
{
$name = basename($dir);
echo ‘’.$name. PHP_EOL.’’;
}
}
[/php]
The current directory is something like this.
images/test
/test02
/test03

But somehow the outcome of this code outputs absolutly no entries in the list, and I have no clue why or how to solve this, I tried this
[php]
if (!is_file($dir) && $dir != ‘.’ && $dir != ‘…’)
[/php] but this however outputs everything, even the files.

I hope someone could help me out with this. It would be greatly appreciated!

Greetings

Yoram Vleugels

Your code should work. But, you have an odd end-of-line in the middle… Try this version:

[php]
$path = ‘…/foto/images/’;
$dirs = scandir($path);

// For testing, let’s print it first, so you can see if the dir counts match
print_r($dirs);

foreach ($dirs as $dir) {
if (is_dir($dir) && $dir != ‘.’ && $dir != ‘…’)
{
echo ‘’ . $dir . ‘
’;
}
}
[/php]
****Well, while testing this for you, I found one problem. If there are not any directories in the folder you are looking at, there will not be any output. I bet that is your problem. the problem is the "is_dir($dir) && part of the code. If you remove it, you should see the pictures in the folder and most likely will not see any sub-folders. Log in to your FTP and make sure you have some dir’s in the dir you are starting at. AND, make sure you are using the correct starting point. Hope that helps!

First thank you for taking the time to help me out!

And then I tried the code with a few minor changes, it worked flawless! On the other hand I’m a bit worried you said there won’t be any output if there aren’t any directories. Because when I tested it it worked brilliantly (that’s done on a mamp pro localhost), I got the list of only the directories and subdirectories and was able to delete them properly. But will this make any unwanted side-effects on the rest of the server and/or php files?

Greetings

Yoram Vleugels

Well, there is a lot of error checking you should add into your code. Mostly, you have routines that do the work, but, no checking to possible errors. Such as if no folders are found, you have to put up some sort of image stating that there are no “albums” found. It would be nice to have a comical picture… Also, if a folder is found, you should check to see of no pictures are in it. If someone creates the folder, but, doesn’t get to upload the pictures. Then, if someone makes a folder, but, never puts any pictures into it. You should delete these after a week or so just to keep the site neat. Then, in your graphics upload section, you have to put some error checking so only the correct type of pictures go there. You can add captcha security so robots don’t dump crap into the server…
Guess there is a lot for you to think about… Good luck with your project… CYA in the bitstream!

Sponsor our Newsletter | Privacy Policy | Terms of Service