96

I am using the following script to read a directory. If there is no file in the directory it should say empty. The problem is, it just keeps saying the directory is empty even though there ARE files inside and vice versa.

<?php
$pid = $_GET["prodref"];
$dir = '/assets/'.$pid.'/v';
$q   = (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty';
    
if ($q=="Empty") 
    echo "the folder is empty"; 
else
    echo "the folder is NOT empty";
?>
AndrewRMillar
  • 608
  • 1
  • 7
  • 17
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

19 Answers19

151

It seems that you need scandir instead of glob, as glob can't see unix hidden files.

<?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) {
  return (count(scandir($dir)) == 2);
}

Note that this code is not the summit of efficiency, as it's unnecessary to read all the files only to tell if directory is empty. So, the better version would be

function dir_is_empty($dir) {
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
    if ($entry != "." && $entry != "..") {
      closedir($handle);
      return false;
    }
  }
  closedir($handle);
  return true;
}

By the way, do not use words to substitute boolean values. The very purpose of the latter is to tell you if something empty or not. An

a === b

expression already returns Empty or Non Empty in terms of programming language, false or true respectively - so, you can use the very result in control structures like IF() without any intermediate values

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 3
    I think both our code is wrong because I removed all files from the folder and it still says the folder is not empty... is there a way to check for hidden files like thumbs.db etc in linux?? – TheBlackBenzKid Sep 21 '11 at 10:15
  • I think the FTP folder is say .. and . in the file is empty. How can I check if and remove the .. and thumbs.db etc?? – TheBlackBenzKid Sep 21 '11 at 10:20
  • glob doesn't support linux hidden files. if you want them you have to go for openir solution like in the deleted answer – Your Common Sense Sep 21 '11 at 10:22
  • it seems you need scandir instead of glob. – Your Common Sense Sep 21 '11 at 10:25
  • The code won't work. None of them. If I have files inside, it says "the folder is NOT empty" and if I removed the files.. it says "the folder is NOT empty".. either way it won't detect – TheBlackBenzKid Sep 21 '11 at 10:28
  • it works for me. try to simplify your code, removing GET part. debug it. var_dump(scandir($dir)) – Your Common Sense Sep 21 '11 at 10:37
  • When I remove GET no error appears. The page is white - blank. – TheBlackBenzKid Sep 21 '11 at 10:40
  • DEBUG IT! sensible! not by just removing code parts at random but reducing it without losing productivity – Your Common Sense Sep 21 '11 at 10:43
  • I am sorry, but I don't understand you. The folder will always contain two files (one .swf and one .jpg file) I have tried different browsers, moved the files around etc and deleted them... what is this debug? – TheBlackBenzKid Sep 21 '11 at 10:46
  • 2
    Do create testing environment. create empty directory in the same folder where script is. make `$dir = 'testfolder';` manually. then run this code. debug is printing out as much information as possible to see what is going wrong. `$dir = 'testfolder';var_dump(scan_dir($dir));` will tell you what is in this directory – Your Common Sense Sep 21 '11 at 11:05
  • Don’t forget to `closedir($handle)` afterwards. – Mathias Bynens Nov 03 '14 at 16:24
  • Worked like a charm. Thanks. much appreciated your effort. +1 up vote. – Prasad Patel Jun 26 '17 at 13:20
  • What is missing in the answer is the fact that there are 3 possible hidden files in a directory. The files '.' and '..' are always there, on Windows and yes also on Mac. On a Mac you may have the file '.DS_Store'. This file is created when the directory has have content. What does this mean? Well simply count the files is not an option because it maybe 2 or 3 files on Mac. So you have to loop over all files in the directory and test for the fileName. See my answer below. – Harm Jun 08 '18 at 17:07
  • is there a reason you chose loose comparison for the `.` & `..`? – FamousAv8er Oct 08 '20 at 16:30
82

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

This works because a new FilesystemIterator will initially point to the first file in the folder - if there are no files in the folder, valid() will return false. (see documentation here.)

As pointed out by abdulmanov.ilmir, optionally check if the directory exists before using the FileSystemIterator because otherwise it'll throw an UnexpectedValueException.

mindplay.dk
  • 7,085
  • 3
  • 44
  • 54
flu
  • 14,307
  • 8
  • 74
  • 71
12

I found a quick solution

<?php
  $dir = 'directory'; // dir path assign here
  echo (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty';
?>
Frank
  • 2,285
  • 7
  • 43
  • 68
7

For a object oriented approach using the RecursiveDirectoryIterator from the Standard PHP Library (SPL).

<?php

namespace My\Folder;

use RecursiveDirectoryIterator;

class FileHelper
{
    /**
     * @param string $dir
     * @return bool
     */
    public static function isEmpty($dir)
    {
        $di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
        return iterator_count($di) === 0;
    }
}

No need to make an instance of your FileHelper whenever you need it, you can access this static method wherever you need it like this:

FileHelper::isEmpty($dir);

The FileHelper class can be extended with other useful methods for copying, deleting, renaming, etc.

There is no need to check the validity of the directory inside the method because if it is invalid the constructor of the RecursiveDirectoryIterator will throw an UnexpectedValueException which that covers that part sufficiently.

Wilt
  • 41,477
  • 12
  • 152
  • 203
6

This is a very old thread, but I thought I'd give my ten cents. The other solutions didn't work for me.

Here is my solution:

function is_dir_empty($dir) {
    foreach (new DirectoryIterator($dir) as $fileInfo) {
        if($fileInfo->isDot()) continue;
        return false;
    }
    return true;
}

Short and sweet. Works like a charm.

narfie
  • 493
  • 1
  • 7
  • 16
6

use

if ($q == "Empty")

instead of

if ($q="Empty")
Toto
  • 89,455
  • 62
  • 89
  • 125
5

I used:

if(is_readable($dir)&&count(scandir($dir))==2) ... //then the dir is empty
Luca C.
  • 11,714
  • 1
  • 86
  • 77
5

Try this:

<?php
$dirPath = "Add your path here";

$destdir = $dirPath;

$handle = opendir($destdir);
$c = 0;
while ($file = readdir($handle)&& $c<3) {
    $c++;
}

if ($c>2) {
    print "Not empty";
} else {
    print "Empty";
} 

?>
Drmzindec
  • 81
  • 1
  • 2
4

@ Your Common Sense

I think your performant example could be more performant using strict comparison:

function is_dir_empty($dir) {
  if (!is_readable($dir)) return null; 
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
    if ($entry !== '.' && $entry !== '..') { // <-- better use strict comparison here
      closedir($handle); // <-- always clean up! Close the directory stream
      return false;
    }
  }
  closedir($handle); // <-- always clean up! Close the directory stream
  return true;
}
André Fiedler
  • 1,093
  • 4
  • 16
  • 25
  • 1
    Good point regarding clean up: The `return false` case is not taking care of it ;-) – Beat Christen Sep 27 '16 at 13:02
  • @BeatChristen Thx for the hint! Fixed it. – André Fiedler Sep 28 '16 at 06:37
  • What I don't understand is why you say that it is better to use strict comparision when comparing to "." and "..". The readdir() function will always return a string (or the false) so I don't see the point. I'd also like to add that cleaning up after yourself is indeed always a good idea, one would think that after return when the $handle variable goes out of scope, closedir() would happen automatically but I just wrote a little test program, and it doesn't. Which is strange because other stuff, like flock do happen automatically. – soger Nov 20 '18 at 17:49
  • @AndréFiedler You are a genius, brilliant and responsible person. In all other answers, they first calculating total number of files and then checking if count > 0. But if a directory contains even one file, it is not empty. – Ashish Doneriya Apr 09 '19 at 16:52
4

Probably because of assignment operator in if statement.

Change:

if ($q="Empty")

To:

if ($q=="Empty")
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Robik
  • 6,047
  • 4
  • 31
  • 41
3

Function count usage maybe slow on big array. isset is ever faster

This will work properly on PHP >= 5.4.0 (see Changelog here)

function dir_is_empty($path){ //$path is realpath or relative path

    $d = scandir($path, SCANDIR_SORT_NONE ); // get dir, without sorting improve performace (see Comment below). 

    if ($d){

        // avoid "count($d)", much faster on big array. 
        // Index 2 means that there is a third element after ".." and "."

        return !isset($d[2]); 
    }

    return false; // or throw an error
}

Otherwise, using @Your Common Sense solution it's better for avoid load file list on RAM

Thanks and vote up to @soger too, to improve this answer using SCANDIR_SORT_NONE option.

Oscar Zarrus
  • 790
  • 1
  • 9
  • 17
  • 3
    As long as we worry about performance, you may want to add the SCANDIR_SORT_NONE parameter too. – soger Sep 09 '20 at 14:52
2

Just correct your code like this:

<?php
    $pid = $_GET["prodref"];
    $dir = '/assets/'.$pid.'/v';
    $q = count(glob("$dir/*")) == 0;

    if ($q) {
        echo "the folder is empty"; 
    } else {
        echo "the folder is NOT empty";
    }
?>
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Alessandro.Vegna
  • 1,262
  • 10
  • 19
1

Even an empty directory contains 2 files . and .., one is a link to the current directory and the second to the parent. Thus, you can use code like this:

$files = scandir("path to directory/");
if(count($files) == 2) {
  //do something if empty
}
Daniil Loban
  • 4,165
  • 1
  • 14
  • 20
rainbowdog
  • 29
  • 1
  • 4
0

I use this method in my Wordpress CSV 2 POST plugin.

    public function does_folder_contain_file_type( $path, $extension ){
        $all_files  = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ) );

        $html_files = new RegexIterator( $all_files, '/\.'.$extension.'/' );  

        foreach( $html_files as $file) {
            return true;// a file with $extension was found
        }   

    return false;// no files with our extension found
}

It works by specific extension but is easily changed to suit your needs by removing "new RegexIterator(" line. Count $all_files.

    public function does_folder_contain_file_type( $path, $extension ){
        $all_files  = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ) );

        return count( $all_files );
    }
Ryan Bayne
  • 162
  • 1
  • 10
0

I had a similar problem recently, although, the highest up-voted answer did not really work for me, hence, I had to come up with a similar solution. and again this may also not be the most efficient way to go about the problem,

I created a function like so

function is_empty_dir($dir)
   {
       if (is_dir($dir))
       {
            $objects = scandir($dir);
            foreach ($objects as $object)
            {
                if ($object != "." && $object != "..")
                {
                    if (filetype($dir."/".$object) == "dir")
                    {
                         return false;
                    } else { 
                        return false;
                    }
                }
            }
            reset($objects);
            return true;
       }

and used it to check for empty dricetory like so

if(is_empty_dir($path)){
            rmdir($path);
        }
user28864
  • 3,375
  • 1
  • 25
  • 19
0

You can use this:

function isEmptyDir($dir)
{
    return (($files = @scandir($dir)) && count($files) <= 2);
}
karrtojal
  • 796
  • 7
  • 16
0

The first question is when is a directory empty? In a directory there are 2 files the '.' and '..'.
Next to that on a Mac there maybe the file '.DS_Store'. This file is created when some kind of content is added to the directory. If these 3 files are in the directory you may say the directory is empty. So to test if a directory is empty (without testing if $dir is a directory):

function isDirEmpty( $dir ) {
  $count = 0;
  foreach (new DirectoryIterator( $dir ) as $fileInfo) {
     if ( $fileInfo->isDot() || $fileInfo->getBasename() == '.DS_Store' ) {
        continue;
     }
     $count++;
  }
  return ($count === 0);
}
Harm
  • 787
  • 7
  • 11
0

@Your Common Sense,@Enyby

Some improvement of your code:

function dir_is_empty($dir) {
    $handle = opendir($dir);
    $result = true;
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            $result = false;
            break 2;
        }
    }
    closedir($handle);
    return $result;
}

I use a variable for storing the result and set it to true.
If the directory is empty the only files that are returned are . and .. (on a linux server, you could extend the condition for mac if you need to) and therefore the condition is true.
Then the value of result is set to false and break 2 exit the if and the while loop so the next statement executed is closedir.
Therefore the while loop will only have 3 circles before it will end regardless if the directory is empty or not.

0
$is_folder_empty = function(string $folder) : bool {
    if (!is_dir($folder))
        return TRUE;

    // This wont work on non linux OS.
    return is_null(shell_exec("ls {$folder}"));
};
$is_folder_empty2 = function(string $folder) : bool {
    if (!is_dir($folder))
        return TRUE;
    
    // Empty folders have two files in it. Single dot and
    // double dot.
    return count(scandir($folder)) === 2;
};

var_dump($is_folder_empty('/tmp/demo'));
var_dump($is_folder_empty2('/tmp/demo'));
Francisco Luz
  • 2,775
  • 2
  • 25
  • 35