The correct way to delete all files older than 2 days in PHP

后端 未结 9 2352
一生所求
一生所求 2020-12-05 01:56

Just curious

        $files = glob(cacheme_directory().\"*\");
        foreach($files as $file)
        {
            $filemtime=filemtime ($file);
                  


        
相关标签:
9条回答
  • 2020-12-05 02:09

    I reckon this is much tidier and easier to read and modify.

    $expire = strtotime('-7 DAYS');
    
    $files = glob($path . '/*');
    
    foreach ($files as $file) {
    
        // Skip anything that is not a file
        if (!is_file($file)) {
            continue;
        }
    
        // Skip any files that have not expired
        if (filemtime($file) > $expire) {
            continue;
        }
    
        unlink($file);
    }
    
    0 讨论(0)
  • 2020-12-05 02:10

    You should add an is_file() check, because PHP normally lists . and .., as well as sub-directories that could reside in the the directory you're checking.

    Also, as this answer suggests, you should replace the pre-calculated seconds with a more expressive notation.

    <?php
      $files = glob(cacheme_directory()."*");
      $now   = time();
    
      foreach ($files as $file) {
        if (is_file($file)) {
          if ($now - filemtime($file) >= 60 * 60 * 24 * 2) { // 2 days
            unlink($file);
          }
        }
      }
    ?>
    

    Alternatively you could also use the DirectoryIterator, as shown in this answer. In this simple case it doesn't really offer any advantages, but it would be OOP way.

    0 讨论(0)
  • 2020-12-05 02:10

    Here is an example of how to do it recursively.

    function remove_files_from_dir_older_than_x_seconds($dir,$seconds = 3600) {
        $files = glob(rtrim($dir, '/')."/*");
        $now   = time();
        foreach ($files as $file) {
            if (is_file($file)) {
                if ($now - filemtime($file) >= $seconds) {
                    echo "removed $file<br>".PHP_EOL;
                    unlink($file);
                }
            } else {
                remove_files_from_dir_older_than_x_seconds($file,$seconds);
            }
        }
    }
    
    remove_files_from_dir_older_than_x_seconds(dirname(__file__).'/cache/', (60 * 60 * 24 * 1) ); // 1 day
    
    0 讨论(0)
  • 2020-12-05 02:11

    Another simplier and more modern way, using FilesystemIterator.

    I'm using 'logs' directory as an example.

    $fileSystemIterator = new FilesystemIterator('logs');
    $now = time();
    foreach ($fileSystemIterator as $file) {
        if ($now - $file->getCTime() >= 60 * 60 * 24 * 2) // 2 days 
            unlink('logs/'.$file->getFilename());
    }
    

    Main advantage is: DirectoryIterator returns virtual directories "." and ".." in a loop. But FilesystemIterator ignores them.

    0 讨论(0)
  • 2020-12-05 02:12

    The easiest way is by using DirectoryIterator:

    <?php
    if (file_exists($folderName)) {
        foreach (new DirectoryIterator($folderName) as $fileInfo) {
            if ($fileInfo->isDot()) {
            continue;
            }
            if ($fileInfo->isFile() && time() - $fileInfo->getCTime() >= 2*24*60*60) {
                unlink($fileInfo->getRealPath());
            }
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-12-05 02:14
    /** It deletes old files.
     *  @param string $dir Directory
     *  @param int $secs Files older than $secs seconds
     *  @param string $pattern Files matching $pattern
     */
    function delete_oldfiles($dir,$secs,$pattern = "/*")
    {
        $now = time();
        foreach(glob("$dir$pattern") as $f) {
          if (is_file($f) && ($now - filemtime($f) > $secs)) unlink($f);
        }
    }
    
    0 讨论(0)
提交回复
热议问题