BorysSokolov Posted December 22, 2012 Share Posted December 22, 2012 Hello. I'm having trouble understanding the below code: <?php $dirName = 'secondDir'; $handle = opendir($dirName); while($printFiles = readdir($handle)){ if($printFiles != '.' && $printFiles != '..'){ echo "<a href = '".$dirName.'/'.$printFiles."'>".$printFiles."</a></br>"; } } closedir($handle); ?> Specifically, it's the if statement that's giving me a hard time. I know what it is, and I know what it does, but I don't understand the logic behind the condition; how does it work to remove the dots before the file list? Are the dots part of the readdir structure? If they are, then how come they can be removed by asking to print out a file list IF they aren't a part of it? I'm not sure if I am making any sense, but if anybody understands, could you please explain this to me? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/272270-noob-question-regarding-reading-directories/ Share on other sites More sharing options...
Solution requinix Posted December 22, 2012 Solution Share Posted December 22, 2012 Every directory has a . and .. entry in it, references to the directory itself and its parent directory respectively. readdir() will return every single thing in the directory including those two. Since you generally don't want to see them in a directory listing the condition is there to make sure that the is only echoed if the filename isn't "." or "..". Quote Link to comment https://forums.phpfreaks.com/topic/272270-noob-question-regarding-reading-directories/#findComment-1400839 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.