• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

PHP Count total files in a directory problem

Hi,

I have a directory with files in it. When I count the total of files using the code below it returns the correct number of visible files.

The problem I have is that when I remove these visible files and empty the directory I seem to keep showing me a count of 1 when the directory appears to be empty.

I know that there are hidden directories ( . and ..) but why do you get a count of one?

The reason I ask is that I want to remove the directory if it is empty of visible files - how do i check that it is empty and then remove the directory?

Thanks,





count(glob($absolutepathtodirectory . "/*"));

Open in new window

0
bigMlittleC
Asked:
bigMlittleC
  • 3
  • 3
  • 2
1 Solution
 
rjdownCommented:
Taken from comments on http://php.net/manual/en/function.glob.php

The ? matches 1 of any character except a /
The * matches 0 or more of any character except a /

So use this instead:
count(glob($absolutepathtodirectory . "/?"));

Open in new window

0
 
Avinash ZalaWeb ExpertCommented:
Try attached function, it will check the for the directory is empty or not.

Also try this method:

// Scans the path for directories and if there are more than 2
// directories i.e. "." and ".." then the directory is not empty
if ( ($files = @scandir('path_to_directory') && (count($files) > 2) )
{
      $directory_not_empty = TRUE;
}


Hope this helps,
Addy
function check_empty_folder ( $folder )
{
	$files = array ();
	if ( $handle = opendir ( $folder ) ) {
		while ( false !== ( $file = readdir ( $handle ) ) ) {
			if ( $file != "." && $file != ".." ) {
				$files [] = $file;
			}
		}
		closedir ( $handle );
	}
	return ( count ( $files ) > 0 ) ? FALSE : TRUE;
}

Open in new window

0
 
rjdownCommented:
Haha Addy, very old-school :D But very inefficient :( glob is fine!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
bigMlittleCAuthor Commented:
rjdown: i have just tested the ? with two files in a directory and got a count of 1 - is that correct? it should be 2?
0
 
bigMlittleCAuthor Commented:
AddyWatson: you solution does work. I will wait to hear back from rjdown before I award the points. I maybe missing something with his/her solution so want to make sure I am being fair. Hope that is OK.
0
 
rjdownCommented:
Sorry, I missed two things here.

1) the fact you're running linux and
2) the other solution posted by Addy

glob works differently on nix systems, especially when it comes to absolute URIs, so for consistency, I recommend you go with Addy's solution of:

if ( ($files = @scandir('path_to_directory') && (count($files) > 2) )
{
      $directory_not_empty = TRUE;
}
0
 
Avinash ZalaWeb ExpertCommented:
Did my solution works for you????


Addy
0
 
bigMlittleCAuthor Commented:
Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now