Jump to content

Noob Question Regarding Reading Directories


BorysSokolov

Recommended Posts

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.

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 "..".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.