GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   php code snippet guru's, please ... (https://gfy.com/showthread.php?t=934575)

Naughty 10-21-2009 01:15 PM

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.

JD 10-21-2009 01:21 PM

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

JD 10-21-2009 01:22 PM

btw i'm not a guru :D

Naughty 10-21-2009 01:24 PM

No, that is my point;-)

AIbenjamink 10-21-2009 01:36 PM

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.

AIbenjamink 10-21-2009 01:54 PM

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($Files3);

  
//Remove the remainder of files
  
foreach($Files as $FilePath=>$ModifiedTime)
  {
      
unlink($FilePath);
  }



calmlikeabomb 10-21-2009 04:15 PM

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...

AIbenjamink 10-21-2009 05:13 PM

Quote:

Originally Posted by calmlikeabomb (Post 16452720)
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..:thumbsup

fatfoo 10-21-2009 06:12 PM

No idea. Good luck.

Naughty 10-22-2009 02:05 AM

Quote:

Originally Posted by AIbenjamink (Post 16452152)
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($Files3);

  
//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?

AIbenjamink 10-22-2009 07:40 AM

Quote:

Originally Posted by Naughty (Post 16453831)
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?

Naughty 10-22-2009 10:26 AM

Quote:

Originally Posted by AIbenjamink (Post 16454483)
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.


All times are GMT -7. The time now is 05:37 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123