简体   繁体   中英

PHP Count through different directories and see which ones have 0 files in them

I have the following folder structure:

images/photo-gallery/2e/
                     72/
                     rk/
                     u3/
                     va/
                     yk/

... and so on. Basically, each time an image is uploaded it hashes the name and then creates a folder with the first two letters. So inside of 2e is 2e0gpw1p.jpg

Here's the thing... if I delete an image, it will delete the file but it will keep the folder that it's in. Now when I have a TON of images uploaded, that will be fine since a lot of images will share the same folder.. but until then, I will end up having a bunch of empty directories.

What I want to do is search through the photo-gallery folder and go through each directory and see which folders are empty.. if there are any empty folders then it will remove it.

I know how to do that for a single directory, like the 2e folder. But how would I do it for all the folders inside the photo-gallery folder?

The PHP function rmdir() will throw a warning if the directory is not empty, so you can use it on non-empty directories without risking deleting them. Combine that with scandir() and array_slice (to remove . and ..), and you can do this:

foreach(array_slice(scandir('images/photo-gallery'),2) as $dir) {
    @rmdir('images/photo-gallery/' . $dir); // use @ to silence the warning
}

while you could do with with php, i'm inclined to use the os for such a task. Of course you can call the below with php

find <parent-dir> -depth -type d -empty -exec rmdir -v {} \;

PLEASE READ THIS WARNING I DID NOT TEST BUT HAVE USED SIMILAR CODE DOZENS OF TIMES. FAMILURIZE YOURSELF WITH THIS AND DO NOT USE IF YOU DO NOT UNDERSTAND WHAT IT IS DOING THIS COULD POTENTIALLY WIPE YOUR SITE FROM THE SERVER.

EDIT BACKUP EVERYTHING BEFORE TRYING THIS YOUR FIRST TIME THE PATH IS VERY VERY IMPORTANT!

Ok with that said this is quite easy :)

<?php
    function recursiveDelete($path){
        $ignore = array(
            'cgi-bin',
            '.',
            '..'
        ); // Directories to ignore

        $dh = opendir($path); // Open the directory

        while(false !== ($file = readdir($dh))){ // Loop through the directory

            if(!in_array($file, $ignore)){ // Check that this file is not to be ignored 

                if(is_dir($path."/".$file)){ // Its a directory, keep going

                    if(!iterator_count(new DirectoryIterator($path."/".$file)))
                        rmdir($path."/".$file); // its empty delete it
                    } else {
                        recursiveDelete($path."/".$file);// Recursive call to self
                    }

                }

            }

        }

        closedir($dh); // All Done close the directory 

    }

    // WARNING IMPROPERLY USED YOU CAN DUMP YOUR ENTIRE SERVER USE WITH CAUTION!!!!
    // I WILL NOT BE HELD RESPONSIBLE FOR MISUSE
    recursiveDelete('/some/directoy/path/to/your/gallery');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM