简体   繁体   English

PHP循环文件夹并删除旧文件/空文件夹

[英]PHP loop trough folders and delete old files/empty folders

I have file system shown in picture.我有如图所示的文件系统。 My task is to loop from temp folder to last subdirectory and delete files that is older than 30 days and also after that delete empty folders.我的任务是从临时文件夹循环到最后一个子目录并删除超过 30 天的文件,然后删除空文件夹。

在此处输入图片说明

I am using "League flysystem/filesystem"我正在使用“联盟飞行系统/文件系统”

Here is what I got now:这是我现在得到的:

use common\components\helper\FileHelper;
use League\Flysystem\StorageAttributes;

class Tmp
{
    /**
     * @throws \League\Flysystem\FilesystemException
     */
    public static function delete($starting_directory, $time_difference): void
    {
        $filesystem = FileHelper::tempFs();
        $expire = strtotime($time_difference);
        $structure = $filesystem->listContents($starting_directory)
            ->toArray();

        foreach ($structure as $data) {
            if ($data->isDir()) {
                $directory_data = $filesystem->listContents($data['path'])
                    ->toArray();
                if (count($directory_data) === 0) {
                    $filesystem->deleteDirectory($data['path']);
                }
                foreach ($directory_data as $directory) {
                    $filesystem->deleteDirectory($directory['path']);
                    self::delete($directory['path'], $time_difference);
                }
            } else if ($data->isFile() && $data['lastModified'] < $expire) {
                $filesystem->delete($data['path']);
            }
        }
    }
}

File updated.文件已更新。 Right now files are deleted after x time and all empty folders except 1st one.现在文件在 x 时间后被删除,除第一个之外的所有空文件夹都被删除。 First folder after temp deletes after second method init.第二种方法初始化后,临时删除后的第一个文件夹。 I guess I am missing a little.我想我错过了一点。

  • every directory have no limit for subdirectories.每个目录对子目录没有限制。
  • Code must accept large amount of files代码必须接受大量文件

Well... After two long days of learning and looking how things going I finally made a progress.嗯...经过两天漫长的学习和了解情况,我终于取得了进展。 I made a script for deleting "X time" old files and also empty folders.我制作了一个用于删除“X 时间”旧文件和空文件夹的脚本。 But still not sure how this script will affect memory with large amount of data.但仍然不确定这个脚本将如何影响大量数据的内存。

Here is solution:这是解决方案:

use common\components\helper\FileHelper;

class Tmp
{
    /**
     * @throws \League\Flysystem\FilesystemException
     */
    public static function delete($starting_directory, $time_difference): void
    {
        $filesystem = FileHelper::tempFs();
        $expire = strtotime($time_difference);
        $structure = $filesystem->listContents($starting_directory)
            ->toArray();

        foreach ($structure as $data) {
            if ($data->isDir()) {
                self::delete($data['path'], $time_difference);
                $directory_data = $filesystem->listContents($data['path'])
                    ->toArray();
                if (count($directory_data) === 0) {
                    $filesystem->deleteDirectory($data['path']);
                }
            } else if ($data->isFile() && $data['lastModified'] < $expire) {
                $filesystem->delete($data['path']);
            }
        }
    }
}

Maybe someone have opinion about possible memory usage running this script?也许有人对运行此脚本可能使用的内存有意见?

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

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