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

bigMlittleCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rjdownCommented:
Haha Addy, very old-school :D But very inefficient :( glob is fine!
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.