简体   繁体   English

PHP通过不同的目录计数,并查看其中包含0个文件的目录

[英]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 所以2e内部是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. 我知道如何对单个目录(例如2e文件夹)执行此操作。 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. 如果目录不为空,PHP函数rmdir()将引发警告,因此您可以在非空目录上使用它,而不必担心将其删除。 Combine that with scandir() and array_slice (to remove . and ..), and you can do this: 将其与scandir()和array_slice结合(以删除。和..),可以执行以下操作:

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. 虽然您可以使用php进行处理,但我倾向于将os用于此类任务。 Of course you can call the below with php 当然,您可以使用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');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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