PHP删除过期目录中的所有子目录?

发布于 2024-10-18 12:59:43 字数 804 浏览 10 评论 0原文

有一个目录/home/example/public_html/users/files/。该目录中有一些具有随机名称的子目录,例如 2378232828923_1298295497

如何彻底删除创建日期>的子目录1个月?

我用一个很好的脚本来删除文件,但它不适用于目录:

$seconds_old = 2629743; //1 month old
$directory = "/home/example/public_html/users/files/";

            if( !$dirhandle = @opendir($directory) )
                        return;

             while( false !== ($filename = readdir($dirhandle)) ) {
                     if( $filename != "." && $filename != ".." ) {
                                $filename = $directory. "/". $filename;

                             if( @filectime($filename) < (time()-$seconds_old) )
                                      @unlink($filename); //rmdir maybe?
                     }
             }

There is a directory /home/example/public_html/users/files/. Within the directory there are subdirectories with random names like 2378232828923_1298295497.

How do I completely delete the subdirectories which have creation date > 1 month?

There is a good script that I use to delete files, but it don't work with dirs:

$seconds_old = 2629743; //1 month old
$directory = "/home/example/public_html/users/files/";

            if( !$dirhandle = @opendir($directory) )
                        return;

             while( false !== ($filename = readdir($dirhandle)) ) {
                     if( $filename != "." && $filename != ".." ) {
                                $filename = $directory. "/". $filename;

                             if( @filectime($filename) < (time()-$seconds_old) )
                                      @unlink($filename); //rmdir maybe?
                     }
             }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩起发的微风 2024-10-25 12:59:43

为此你需要一个递归函数。

function remove_dir($dir)
{
    chdir($dir);
    if( !$dirhandle = @opendir('.') )
        return;

    while( false !== ($filename = readdir($dirhandle)) ) {
        if( $filename == "." || $filename = ".." )
            continue;

        if( @filectime($filename) < (time()-$seconds_old) ) {
            if (is_dir($filename)
                remove_dir($filename);
            else 
                @unlink($filename);
        }
    }
    chdir("..");
    rmdir($dir);
}

you need a recursive function for this.

function remove_dir($dir)
{
    chdir($dir);
    if( !$dirhandle = @opendir('.') )
        return;

    while( false !== ($filename = readdir($dirhandle)) ) {
        if( $filename == "." || $filename = ".." )
            continue;

        if( @filectime($filename) < (time()-$seconds_old) ) {
            if (is_dir($filename)
                remove_dir($filename);
            else 
                @unlink($filename);
        }
    }
    chdir("..");
    rmdir($dir);
}
月亮是我掰弯的 2024-10-25 12:59:43
 <?php
    $dirs = array();
    $index = array();
    $onemonthback = strtotime('-1 month');
    $handle = opendir('relative/path/to/dir');
    while($file = readdir($handle){
        if(is_dir($file) && $file != '.' && $file != '..'){
            $dirs[] = $file;
                $index[] = filemtime( 'relative/path/to/dir/'.$file );
        }
}    
closedir($handle);


    asort( $index );

    foreach($index as $i => $t) {

        if($t < $onemonthback) {
            @unlink('relative/path/to/dir/'.$dirs[$i]);
        }


}
?>
 <?php
    $dirs = array();
    $index = array();
    $onemonthback = strtotime('-1 month');
    $handle = opendir('relative/path/to/dir');
    while($file = readdir($handle){
        if(is_dir($file) && $file != '.' && $file != '..'){
            $dirs[] = $file;
                $index[] = filemtime( 'relative/path/to/dir/'.$file );
        }
}    
closedir($handle);


    asort( $index );

    foreach($index as $i => $t) {

        if($t < $onemonthback) {
            @unlink('relative/path/to/dir/'.$dirs[$i]);
        }


}
?>
温柔嚣张 2024-10-25 12:59:43

如果 PHP 在 Linux 服务器上运行,您可以使用 shell 命令来提高性能(递归 PHP 函数在非常大的目录中可能效率低下):

shell_exec('rm -rf '.$directory);

If PHP runs on a Linux server, you could use a shell command, to improve performance (a recursive PHP function can be inefficient in very large directories):

shell_exec('rm -rf '.$directory);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文