dongzhan2461 2013-01-06 14:24
浏览 17
已采纳

too long

I have a 'cache' folder on my webserver where .html files are stored, the file structure is as follows:

cache > user@gmail.com > several .html files.

I need a PHP script that will browse all the subdirectors within the user folders and delete files older than 3 months.

I have this script so far:

$DIR = '/cache/';
if ($handle = opendir($DIR)) {

    while (false !== ($file = readdir($handle))) {
        if ( filemtime($DIR.$file) <= time()-60*60*24*120 ) { //120 days?
           unlink($DIR.$file);
        }
    }

    closedir($handle);
}

But it does not handle sub-directories. It is also giving me errors because it is trying to unlink directorys..

Update: trying to add a count feature:

<?php

rrmdir(/www/deletecontentsofthisfolder/);
echo $count . ' files deleted!';

function rrmdir($dir,$count=0) 
{
    if (is_dir($dir)) 
    {
        $objects = scandir($dir);
        foreach ($objects as $object) 
        {
            if ($object != "." && $object != "..") 
            {
                if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object,$count);
                if (filemtime($dir."/".$object) <= time()-60*60*24*120) @unlink($dir."/".$object); count++
            }
        }
        reset($objects);
        //rmdir($dir);
    }
return $count;
}
  • 写回答

1条回答 默认 最新

  • douqiang5933 2013-01-06 14:32
    关注
    function rrmdir($dir) 
    {
        if (is_dir($dir)) 
        {
            $objects = scandir($dir);
            foreach ($objects as $object) 
            {
                if ($object != "." && $object != "..") 
                {
                    if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object);
                    if (filemtime($dir."/".$object) <= time()-60*60*24*120) @unlink($dir."/".$object);
                }
            }
            reset($objects);
            //rmdir($dir);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比