tex1820 Posted July 7, 2008 Share Posted July 7, 2008 Hi all, my name is tex. I'm new to php. I started to write my first real self made php script however im starting to run into problums. here is what I have so far. The top part of the code came from this website http://snipplr.com/view/4147/count-number-of-files-in-a-directory/ I am using it to create a simple web gallery. here is what I have so far: <? $dir = "gallery"; $count = 0; if(is_dir($dir)) { if($handle = opendir($dir)) { while(($file = readdir($handle)) !== false) { $count++; } closedir($handle); } } echo $count; while ($count > '0') { ?> <a href="gallery/<?php echo $count; ?>.gif"><img src="gallery/<?php echo $count; ?>.gif"></a> <? $count--; } ?> Now the thing is, it is finding the images but it says there are 5 images, however there are only 3 in the dir. here is another example: http://monty.triplethreatairsoft.com/gallerymessedexample.php I left the number to show that it is seeing 5 files. Also why will the images not load? I have a shot in the dark, the php stops loading the images when it loops. am I right about that? how can i fix it? thanks everyone, when it is 100% done, I will be shour to share it Tex Link to comment https://forums.phpfreaks.com/topic/113562-simple-photo-gallery/ Share on other sites More sharing options...
rhodesa Posted July 7, 2008 Share Posted July 7, 2008 when you are reading the list of files in a directory (either with readdir() or scandir()) you will always get 2 'extra' entries. They are '.' and '..' . => Current directory .. => Parent directory you need to add logic in your code to skip these entries. i usually skip anything that starts with a period since (for the most part) anything that starts with a period should be a system/hidden/etc file <?php //I would recommend using full PHP tags as not all PHP installs will have short tags enabled //Also, try getting in the habit of indenting...it's much easier to read $dir = "gallery"; $count = 0; if(is_dir($dir)) { if($handle = opendir($dir)) { while(($file = readdir($handle)) !== false) { if($file{0} == '.') continue; //This line skips files that start with a period echo "<a href=\"{$gallery}/{$file}\"><img src=\"{$gallery}/{$file}\"></a>\n"; } closedir($handle); } } echo $count; ?> Link to comment https://forums.phpfreaks.com/topic/113562-simple-photo-gallery/#findComment-583542 Share on other sites More sharing options...
tex1820 Posted July 9, 2008 Author Share Posted July 9, 2008 wow i did not know that XD well thank you m8 Link to comment https://forums.phpfreaks.com/topic/113562-simple-photo-gallery/#findComment-585358 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.