hitchhiker Posted December 13, 2006 Share Posted December 13, 2006 Hi, I'm using the following code to list the files in a directory. The problem is that besides the correct files it also returns two extra files named '.' and '..'! This is the code: <?php $direct = "image/"; if ($handle = opendir('images/')) { while (false !== ($file = readdir($handle))) { echo ("<p>" . $file . "</p>"); } closedir($handle); } ?> And it returns this: . .. image1.gif image2.gif etc. I can't understand what the '.' and '..' are there for as they are not in the actual directory on the server? Also, if there's nothing I can do about this, how would I get round it i.e. ignore anything found called '.' or '..'. Link to comment Share on other sites More sharing options...
sallonoroff Posted December 13, 2006 Share Posted December 13, 2006 I'm sure a more UNIX/Linux-savvy person will have a better explanation, but those dots represent the current level and parent level directories... So your code is doing exactly as it is told... . Link to comment Share on other sites More sharing options...
evildrneil Posted December 13, 2006 Share Posted December 13, 2006 The . is the current directory and the .. is the parent directory - they are automagic parts of the linux directory listing. Try using: if ($file != "." && $file != "..") { echo "<p> $file </p>\n"; } rather than your echo statement. Link to comment Share on other sites More sharing options...
hitchhiker Posted December 13, 2006 Author Share Posted December 13, 2006 Brilliant! Thanks - works like a treat! <?php $direct = "image/"; if ($handle = opendir('images/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo ("<p>" . $file . "</p>"); } } closedir($handle); } ?> Link to comment Share on other sites More sharing options...
dosxuk Posted December 13, 2006 Share Posted December 13, 2006 Just to point out these directories also exist under windows. Link to comment Share on other sites More sharing options...
expitlad Posted December 13, 2006 Share Posted December 13, 2006 Hitchhiker Good Luck with the site. I've just downloaded the Parks guide. Cheers p.s.....visited one of your adsense 'sponsors' Link to comment Share on other sites More sharing options...
hitchhiker Posted December 13, 2006 Author Share Posted December 13, 2006 Hitchhiker Good Luck with the site. I've just downloaded the Parks guide. Cheers p.s.....visited one of your adsense 'sponsors' Thanks!!! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.