Jump to content

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


eldan88
Go to solution Solved by Christian F.,

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;
}

?>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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) {
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.