Go Back   WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk
Register Blogs FAQ Social Groups CalendarHelp Desk

Reply
 
Share
LinkBack Thread Tools
Old 02-11-2012, 11:11 PM   #1
Senior Warrior Member
War Room Member
 
Terry Crim's Avatar
 
Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
Default PHP: quick way to check folder empty?

PHP 5.2.x

What is a quick way to check if a folder is empty? I have a routine that empty's out a folder but I want to check if it is empty before I run the deletion code.

Searching the SE's so far I haven't found a quick and easy way to do this without writing a long multi-line function.

Any ideas?


Thanks.

Terry Crim is offline   Reply With Quote
Old 02-12-2012, 12:00 AM   #2
Senior Warrior Member
War Room Member
 
Terry Crim's Avatar
 
Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
Default Re: PHP: quick way to check folder empty?

Ok, I wrote the below code to achieve this. Is there a simpler way to do this I have missed?

PHP Code:

$files 
scandir($dir);
unset(
$files[array_search(".",$files)]);
unset(
$files[array_search("..",$files)]);

If(
count($files) > 0) {
  
DELETE FILES CODE HERE


Terry Crim is offline   Reply With Quote
Old 02-13-2012, 06:09 AM   #3
Warrior Member
 
Join Date: Oct 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: PHP: quick way to check folder empty?

you can do it with
isEmptyDir($dir){ function easily

jaredrasc is offline   Reply With Quote
Old 02-13-2012, 07:14 PM   #4
Warrior Member
 
Join Date: Feb 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default Re: PHP: quick way to check folder empty?

Ah this will help with my project, atm i search for all the files in the folder. checking it actually has something will help
JoeyJoe is offline   Reply With Quote
Old 02-13-2012, 07:35 PM   #5
Senior Warrior Member
War Room Member
 
Terry Crim's Avatar
 
Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
Default Re: PHP: quick way to check folder empty?

Quote:
Originally Posted by jaredrasc View Post
you can do it with
isEmptyDir($dir){ function easily

Where is isEmptyDir ? Checking the manual that function does not exist, at least not in the 5.x and 5.3.X manuals.

Is this custom function you created?

Edit: OH, you mean create a function around that code I wrote above? Yeah, but I was wondering if there is an easier way to do this than how I went about it above. Writing modular code is important but I did not know if there was something already that exists that can do this simpler than how I went about it above.

Thanks for your response.

Terry Crim is offline   Reply With Quote
Old 02-13-2012, 07:51 PM   #6
Senior Warrior Member
War Room Member
 
Terry Crim's Avatar
 
Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
Default Re: PHP: quick way to check folder empty?

Quote:
Originally Posted by JoeyJoe View Post
Ah this will help with my project, atm i search for all the files in the folder. checking it actually has something will help

Yes, I did not know if you realized this or not but the function

PHP Code:
scandir() 
actually creates an array with all the files in a specific folder, if you look up that function in the help file or on php.net you will see the options for this function and scanning through the comments you will find ways to get the entire folder tree as well.

I find using things like this is easier than using file open and close code etc...

In my sample code above I take out the "." and ".." directory links from the returned array if they happen to exist. I am not sure when or if they would not exist but anything is possible so I allowed for that in my unset commands. In my script this code is never seen so I don't really care if errors happen with files not existing because the error will never be seen but if you want to tidy this up you can do some more checking OR just use @ to silence the errors if any occur. I didn't care in my situation so I didn't do any error checking at all.



Terry Crim is offline   Reply With Quote
Reply

  WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk

Bookmarks

Tags
check, empty, folder, php, quick

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -6. The time now is 06:47 PM.