Jump to content

Recommended Posts

Hello phpfreaks ..

I'm trying to learn php .. and now I've made a small function which looks in the folder, the folder I empty it should write "The folder is empty", and if the folder is not empty, it should write "folder is not empty". But no matter how many Filder are in the folder, get the same result "folder is not empty" ..

I hope someone can help me and tell what I'm doing wrong.

Here is my function:

<?php

function insertFile ($imageFunction){

// First make sure the folder is empty

        $dh = opendir($imageFunction['dir']);

 

// Create a array for all file names in the folder

        $files = array();

        $files[] = readdir($dh);     

       

// If the count of filenames is more than 0       

        if(count($file) > 0) {

            echo 'The folder is not empty';

        }else{

            echo 'The folder is empty.';

        }

       

        echo count($file);

 

   

    closedir($dh);

   

}

?>

Postbil.com

 

Link to comment
https://forums.phpfreaks.com/topic/195920-empty-folder/
Share on other sites

Have a look at this:

 

function is_empty_folder($dir) {
if (is_dir($dir)) {
   $dl=opendir($dir);
   if ($dl) {
       while($name = readdir($dl)) {
   if (!is_dir("$dir/$name")) { //<--- corrected here
       return false;
       break;
       }
   }
       closedir($dl);
       }
   return true;
   } else return true;
}

Link to comment
https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029145
Share on other sites

Or you can strip out those files from being seen:

 

if(is_dir($dir)){
  $dir_array = scandir($dir);
  foreach($dir_array as $file) {
    if(stripos($file, '.') > 0) {
    echo "There are files in the folder";
    } else {
     echo "There are no files";
}
}
}

 

 

folder "." is go back 1 folder back, like u are at c:\windows\system32, it will go back to c:\windows

folder ".." is go back to root, i think, so, u will be at c:\ after u clicked it

 

 

most files has extensions, so, your way just strip many files out too

Link to comment
https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029331
Share on other sites

Or you can strip out those files from being seen:

 

if(is_dir($dir)){
  $dir_array = scandir($dir);
  foreach($dir_array as $file) {
    if(stripos($file, '.') > 0) {
    echo "There are files in the folder";
    } else {
     echo "There are no files";
}
}
}

 

 

folder "." is go back 1 folder back, like u are at c:\windows\system32, it will go back to c:\windows

folder ".." is go back to root, i think, so, u will be at c:\ after u clicked it

 

 

most files has extensions, so, your way just strip many files out too

 

I have used this method many different times on many different projects and it has never stripped out any proper files out of the count

Link to comment
https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029346
Share on other sites

 

folder "." is go back 1 folder back, like u are at c:\windows\system32, it will go back to c:\windows

folder ".." is go back to root, i think, so, u will be at c:\ after u clicked it

 

 

No. dot refers to the current folder. dot-dot refers to the parent folder.

And tell me, how would you ever click on one. The file system itself uses them for traversing the directory tree.

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029348
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.