Because this has some overhead, I'm going to suggest that you only do this from PHP if absolutely needed. You can use the Linux find
command for this:
find /path/to/dir -empty -type d -delete
I would put that in a cron job. If you like you can use the php system command to do this though:
system('find /path/to/dir -empty -type d -delete', $retval);
Using this you would simply delete the file and then let this run only every day or so to go through and take care of any empty directories. This may seem more hackish than making it all in PHP but it'll run much faster. It is less portable but that shouldn't matter too much for most sites. Save this as rdel.bat
(I walways make sure you have the [Show hidden file extensions][2]
explorer folder option turned on but if you don't then use the drop-down in your text editors Save As...
dialog to ensure it has the proper extension).
UPDATE:
To do the same thing in Windows use a batch file with just this one line:
for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"
Test this by changing to a directory and running your new file. It should remove any empty directories below the current.
To schedule this just add an entry to your task scheduler. That is a little different depending on which version of Windows you use. Be sure to set the working directory correctly (this is how the batch file knows where to start).
The problem with this is that in Windows a lot of junk files get created in empty directories (ie Thumbs.db). You can handle that by adding code to your batch file to remove any of those files too:
del /s /q Thumbs.db
Add this above the other line and repeat for anything else which may be unneeded.