Page 1 of 1
checking if a directory is empty
Posted: Wed Nov 08, 2006 8:59 am
by hame22
Hi
I am attempting to do some validating when removing a directory using rmdir.
How can i validate whether or not a directory is empty of files?
thanks in advance
Re: checking if a directory is empty
Posted: Wed Nov 08, 2006 9:01 am
by Chris Corbyn
hame22 wrote:Hi
I am attempting to do some validating when removing a directory using rmdir.
How can i validate whether or not a directory is empty of files?
thanks in advance
Assuming glob() finds hidden files....
Code: Select all
if (count(glob($dir.'/*')) > 2)
{
//Directory is not empty
}
Posted: Wed Nov 08, 2006 10:00 am
by printf
On windows, none of the PHP functions can see hidden files or folders, you have to use CMD or the COM file scripting object to access that information to delete or change those so called permissions.
printf
Posted: Wed Nov 08, 2006 10:03 am
by hame22
I have managed to solve the problem using the following code:
Code: Select all
if($checkDir = opendir($remove_dir)){
$cFile = 0;
while($file = readdir($checkDir)){
if($file != "." && $file != ".."){
if(is_dir($dir . "/" . $file)){
$cFile++;
}
else{
$cFile++;
}
}
}
}
thanks alot for your suggestions
Posted: Wed Nov 08, 2006 11:57 pm
by feyd
printf wrote:On windows, none of the PHP functions can see hidden files or folders, you have to use CMD or the COM file scripting object to access that information to delete or change those so called permissions.
printf
ahem:
Code: Select all
[feyd@home]>copy con hiddenfile.txt
hi
^Z
1 file(s) copied.
[feyd@home]>attrib +h hiddenfile.txt
[feyd@home]>attrib hiddenfile.txt
A H C:\Web\tests\hiddenfile.txt
[feyd@home]>php -r "var_dump(glob('./*'));" | grep "hidden"
string(16) "./hiddenfile.txt"
[feyd@home]>php -v
PHP 5.1.5 (cli) (built: Aug 15 2006 23:54:56)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
with Xdebug v2.0.0beta6, Copyright (c) 2002, 2003, 2004, 2005, 2006, by Derick Rethans