Jump to content

Need help checking to see if a directory is empty or not


eldan88

Recommended Posts

Hey,

 

  I was doing some research to see on how I can check to see if a directory is empty or not, and came across this code. I am a little bit confused as to why they put "while (false !== " on the while statement. Can anyone help me understand that?



<?php
$pid = basename($_GET["prodref"]); //let's sanitize it a bit
$dir = "/assets/$pid/v";

if (is_dir_empty($dir)) {
  echo "the folder is empty"; 
}else{
  echo "the folder is NOT empty";
}


function is_dir_empty($dir) {
  if (!is_readable($dir)) return NULL; 
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
    if ($entry != "." && $entry != "..") {
      return FALSE;
    }
  }
  return TRUE;
}

?>

The "false !=" is usually implied by the fact that if the loop returns a value that can be cast to boolean false, it usually means that it is false. In this instance, however, the author of the code has opted to using the "identical to" operator, in order to allow files/directories with an empty name or named "0".

The "false !=" is usually implied by the fact that if the loop returns a value that can be cast to boolean false, it usually means that it is false. In this instance, however, the author of the code has opted to using the "identical to" operator, in order to allow files/directories with an empty name or named "0".

Okay i got it now.

 

Does that mean that readir call back is only true or false?

No, that means that readdir can return a number of values, of which some can be evaluated to "false".

 

Check out the type autocasting/juggling section of the PHP manual for more detailed information.

 

Okay  thank you. I actually just wanted to check if the files exists in one directory, therefore I changed the function. Does this look correct?

function dir_not_empty($dir) {
  if (!is_readable($dir)) { return NULL;} 
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
    if ($entry == "." ) {return true;} 
  }// End of the while statment
}// End of function is_dir_not_empty($dir) {

Read through what that code does*, out loud in plain English, and you should be able to answer that question yourself. Doing this will help you increase your understanding of the code, and thus become a better programmer.

 

* What it actually does, not what you intend/hope for it to do.

Read through what that code does*, out loud in plain English, and you should be able to answer that question yourself. Doing this will help you increase your understanding of the code, and thus become a better programmer.

 

* What it actually does, not what you intend/hope for it to do.

 

I agree. I'm just having a hard time understanding what the "." means for if ($entry == "." )

"." is a directory link for "current directory", ".." is the same for the parent folder. Two items that are present in all directories, as a control structure for utility purposes.

 

Just type "dir" on your Widows computer, or "ls -al" on a *nix-based OS, and you'll see.

"." is a directory link for "current directory", ".." is the same for the parent folder. Two items that are present in all directories, as a control structure for utility purposes.

 

Just type "dir" on your Widows computer, or "ls -al" on a *nix-based OS, and you'll see

Okay got it! Thanks!

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.