Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

How to check if directory is empty or not in PHP

5 Answers

0 votes
/*
array glob ( string $pattern [, int $flags = 0 ] )
*/

if (count(glob("e:/xampp/ *")) !== 0) 
    echo "directory is not empty";
else
    echo "directory is empty";
 
 
/*
run:
 
directory is not empty
 
*/

 



answered Dec 19, 2015 by avibootz
edited Dec 19, 2015 by avibootz
0 votes
/*
array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING 
                                  [, resource $context ]] )
*/
  
$files_and_directories = scandir('e:/xampp');
if (count($files_and_directories) <= 2) // First items are . and ..
    echo "directory is empty";
else
    echo "directory is not empty";
  
/*
run:
  
Directory not empty
  
*/

 



answered Dec 19, 2015 by avibootz
edited Dec 19, 2015 by avibootz
0 votes
/*
array glob ( string $pattern [, int $flags = 0 ] )
*/
 
if (count(glob("e:/xampp/ *", GLOB_NOSORT)) !== 0) 
    echo "directory is not empty";
else
    echo "directory is empty";
  
  
/*
run:
  
directory is not empty
  
*/

 



answered Dec 19, 2015 by avibootz
0 votes
/*
array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING 
                                  [, resource $context ]] )
                                  
                                  
bool is_readable ( string $filename )                                  
*/
 
function dir_is_empty($dir) 
{
  // Tells whether a file exists and is readable
  if (!is_readable($dir)) return NULL; 
  
  return (count(scandir($dir)) == 2); //  First items are . and ..
}
 
  
if (dir_is_empty("e:/xampp")) 
    echo "directory is empty";
else
    echo "directory is not empty";
  
/*
run:
  
directory is not empty
  
*/

 



answered Dec 19, 2015 by avibootz
0 votes
/*
 FilesystemIterator extends DirectoryIterator implements SeekableIterator { ... }
 
 // Check whether current DirectoryIterator position is a valid file
 public bool DirectoryIterator::valid ( void )
*/
 
if (!(new \FilesystemIterator("e:/test"))->valid()) 
    echo "directory is empty";
else
    echo "directory is not empty";
  
/*
run:
  
directory is empty
  
*/

 



answered Dec 19, 2015 by avibootz

Related questions

1 answer 135 views
3 answers 218 views
3 answers 246 views
1 answer 227 views
1 answer 175 views
2 answers 81 views
...