php code snippet guru's, please ...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naughty
    Confirmed User
    • Jul 2001
    • 6487

    #1

    php code snippet guru's, please ...

    I have this to kill some older files, backups...

    PHP Code:
    $dir = '/www/';
    if ($handle = opendir($dir)) {
    
      while (false !== ($file = readdir($handle))) {
        if ($file[0] == '.' || is_dir("$dir/$file")) {
          continue;
        }
        if ((time() - filemtime($file)) > ($days *86400)) { //7 days
          unlink("$dir/$file");
        }
      }
      closedir($handle);
    } 
    

    Any idea how i can change this
    "kill all older than 7 days",
    into
    "kill all BUT the latest three" <- so, could be there's one in there that is weeks old, as long as no new ones appear

    I have had TM3 break and the kill older than x days backups. When we found out TM3 was broken, it had also killed all good backups already

    Anyone? We're going to cron this.
    seks.ai for sale - ping me
  • JD
    Too lazy to set a custom title
    • Sep 2003
    • 22651

    #2
    can't you just change the > to <= and adjust the time?

    Comment

    • JD
      Too lazy to set a custom title
      • Sep 2003
      • 22651

      #3
      btw i'm not a guru :D

      Comment

      • Naughty
        Confirmed User
        • Jul 2001
        • 6487

        #4
        No, that is my point;-)
        seks.ai for sale - ping me

        Comment

        • AIbenjamink
          Confirmed User
          • Jan 2009
          • 420

          #5
          You could place the file modified times, and file paths in an array. Sort by file modified times, remove the 3 most recent from the array, and recursively remove the remainder of the files.
          Benjamin : [email protected] : 405-243-447 : www.AdultInterface.com

          Comment

          • AIbenjamink
            Confirmed User
            • Jan 2009
            • 420

            #6
            Here you go. Hasn't been tested:

            PHP Code:
            $dir = '/www/';
            $Files=array();
            
            if ($handle = opendir($dir)) {
            
              while (false !== ($file = readdir($handle))) {
                if ($file[0] == '.' || is_dir("$dir/$file")) {
                  continue;
                }
                if ((time() - filemtime($file)) > ($days *86400)) { //7 days
                  //unlink("$dir/$file");
                  $Files[$dir/$file]=filemtime($file);
                }
              }
              closedir($handle);
            
              //Sort the array by file modified times, reverse sort
              arsort($Files);
            
              //Remove the newest 3 files from the array
              $Files=array_slice($Files, 3);
            
              //Remove the remainder of files
              foreach($Files as $FilePath=>$ModifiedTime)
              {
                  unlink($FilePath);
              }
            } 
            
            Benjamin : [email protected] : 405-243-447 : www.AdultInterface.com

            Comment

            • calmlikeabomb
              Confirmed User
              • May 2004
              • 1323

              #7
              One thing I noticed about all the code posted in this thread is that it will always delete every file, because $days isn't set anywhere, which means the conditional if(time() - filemtime($file)) > ($days *86400) is always gonna be true, because 0 times any number is always 0...
              subarus.

              Comment

              • AIbenjamink
                Confirmed User
                • Jan 2009
                • 420

                #8
                Originally posted by calmlikeabomb
                One thing I noticed about all the code posted in this thread is that it will always delete every file, because $days isn't set anywhere, which means the conditional if(time() - filemtime($file)) > ($days *86400) is always gonna be true, because 0 times any number is always 0...
                Noticed that, assumed he probably defined before his attached code snippet.

                Good looking out though..
                Benjamin : [email protected] : 405-243-447 : www.AdultInterface.com

                Comment

                • fatfoo
                  ICQ:649699063
                  • Mar 2003
                  • 27763

                  #9
                  No idea. Good luck.
                  Send me an email: [email protected]

                  Comment

                  • Naughty
                    Confirmed User
                    • Jul 2001
                    • 6487

                    #10
                    Originally posted by AIbenjamink
                    Here you go. Hasn't been tested:

                    PHP Code:
                    $dir = '/www/';
                    $Files=array();
                    
                    if ($handle = opendir($dir)) {
                    
                      while (false !== ($file = readdir($handle))) {
                        if ($file[0] == '.' || is_dir("$dir/$file")) {
                          continue;
                        }
                        if ((time() - filemtime($file)) > ($days *86400)) { //7 days
                          //unlink("$dir/$file");
                          $Files[$dir/$file]=filemtime($file);
                        }
                      }
                      closedir($handle);
                    
                      //Sort the array by file modified times, reverse sort
                      arsort($Files);
                    
                      //Remove the newest 3 files from the array
                      $Files=array_slice($Files, 3);
                    
                      //Remove the remainder of files
                      foreach($Files as $FilePath=>$ModifiedTime)
                      {
                          unlink($FilePath);
                      }
                    } 
                    
                    Thanks, will look into it. However, you say this:
                    //Remove the newest 3 files from the array

                    Obviously we want to kill the oldest, not the newest, right?

                    So how does that come about?
                    seks.ai for sale - ping me

                    Comment

                    • AIbenjamink
                      Confirmed User
                      • Jan 2009
                      • 420

                      #11
                      Originally posted by Naughty
                      Thanks, will look into it. However, you say this:
                      //Remove the newest 3 files from the array

                      Obviously we want to kill the oldest, not the newest, right?

                      So how does that come about?
                      Exactly, the "//Remove the remainder of files" section physically removes the files that are still in the array of files obtained from the folder. The "//Remove the newest 3 files from the array" section ensures the newest 3 files are not in the array of files to be physically removed.

                      Does that make sense?
                      Benjamin : [email protected] : 405-243-447 : www.AdultInterface.com

                      Comment

                      • Naughty
                        Confirmed User
                        • Jul 2001
                        • 6487

                        #12
                        Originally posted by AIbenjamink
                        Exactly, the "//Remove the remainder of files" section physically removes the files that are still in the array of files obtained from the folder. The "//Remove the newest 3 files from the array" section ensures the newest 3 files are not in the array of files to be physically removed.

                        Does that make sense?
                        yes, thanks.
                        seks.ai for sale - ping me

                        Comment

                        Working...