![]() | ![]() | ||||||||
| | #1 |
| Senior Warrior Member War Room Member Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
|
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. |
| | |
| | |
| | #2 |
| Senior Warrior Member War Room Member Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
|
Ok, I wrote the below code to achieve this. Is there a simpler way to do this I have missed? PHP Code: |
| | |
| | |
| | #3 |
| Warrior Member Join Date: Oct 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
you can do it with isEmptyDir($dir){ function easily |
| | |
| | |
| | #4 |
| Warrior Member Join Date: Feb 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
|
Ah this will help with my project, atm i search for all the files in the folder. checking it actually has something will help
|
| | |
| | #5 |
| Senior Warrior Member War Room Member Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
| 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. |
| | |
| | |
| | #6 | |
| Senior Warrior Member War Room Member Join Date: Jul 2002
Posts: 2,650
Thanks: 16
Thanked 69 Times in 55 Posts
| Quote:
Yes, I did not know if you realized this or not but the function PHP Code: 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. | |
| | ||
| | |
![]() |
|
| Bookmarks |
| Tags |
| check, empty, folder, php, quick |
| Thread Tools | |
| |
![]() |