Jump to content

Noob Question Regarding Reading Directories


Go to solution Solved by requinix,

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.

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.