j.smith1981 Posted August 12, 2011 Share Posted August 12, 2011 Hi there I am having a problem outputting file names in PHP using readdir() function. The problem is with this code below: <?php if(is_dir($directory)) { if($dh = opendir($directory)) { while(($file = readdir($dh)) !== false) { echo $file; } closedir($dh); } } ?> With $directory obviously being the directory I wanted to output allowing me to let users say change the style sheets they where using, going through a book of php work you see in Hacks (meaning of course making PHP do things you didn't realise where possible or knew how to do, or a very hit and miss approach to getting work done as quickly as possible and the like), it's really expanding my capabilities in PHP. Though everytime I try and output the file names in which ever directory there's this kind of output: .myfilename.extension.. Where there's one leading fullstop/period (.) and then 2 trailing ones, is there any bette method that gets rid of these entirely please? Would really appreciate your help on this and thank you for your help in advance too! Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/244598-problems-outputting-file-names-using-readdir/ Share on other sites More sharing options...
AyKay47 Posted August 12, 2011 Share Posted August 12, 2011 the . and the .. are actually directory references as well, since you did not tell the script to add a newline for each directory.. <?php if ($handle = opendir('.')) { //insert directory while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "$file\n"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244598-problems-outputting-file-names-using-readdir/#findComment-1256329 Share on other sites More sharing options...
j.smith1981 Posted August 12, 2011 Author Share Posted August 12, 2011 Ahh ok thank you ever so much! I mean the example in the book made sense to me however I was going off the PHP manual to really get to grips with the books example in Hacks in PHP Thanks ever so much again though, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/244598-problems-outputting-file-names-using-readdir/#findComment-1256330 Share on other sites More sharing options...
AyKay47 Posted August 12, 2011 Share Posted August 12, 2011 no problem.. Quote Link to comment https://forums.phpfreaks.com/topic/244598-problems-outputting-file-names-using-readdir/#findComment-1256332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.