Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 10-21-2009, 01:15 PM   #1
Naughty
Confirmed User
 
Industry Role:
Join Date: Jul 2001
Location: Utopia
Posts: 6,479
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
Naughty is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 01:21 PM   #2
JD
Too lazy to set a custom title
 
Industry Role:
Join Date: Sep 2003
Posts: 22,651
can't you just change the > to <= and adjust the time?
JD is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 01:22 PM   #3
JD
Too lazy to set a custom title
 
Industry Role:
Join Date: Sep 2003
Posts: 22,651
btw i'm not a guru :D
JD is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 01:24 PM   #4
Naughty
Confirmed User
 
Industry Role:
Join Date: Jul 2001
Location: Utopia
Posts: 6,479
No, that is my point;-)
__________________
seks.ai for sale - ping me
Naughty is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 01:36 PM   #5
AIbenjamink
Confirmed User
 
AIbenjamink's Avatar
 
Industry Role:
Join Date: Jan 2009
Posts: 420
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

AIbenjamink is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 01:54 PM   #6
AIbenjamink
Confirmed User
 
AIbenjamink's Avatar
 
Industry Role:
Join Date: Jan 2009
Posts: 420
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);
  }

__________________
Benjamin : [email protected] : 405-243-447 : www.AdultInterface.com

AIbenjamink is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 04:15 PM   #7
calmlikeabomb
Confirmed User
 
calmlikeabomb's Avatar
 
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
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.
calmlikeabomb is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 05:13 PM   #8
AIbenjamink
Confirmed User
 
AIbenjamink's Avatar
 
Industry Role:
Join Date: Jan 2009
Posts: 420
Quote:
Originally Posted by calmlikeabomb View Post
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

AIbenjamink is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-21-2009, 06:12 PM   #9
fatfoo
ICQ:649699063
 
Industry Role:
Join Date: Mar 2003
Posts: 27,763
No idea. Good luck.
__________________
Send me an email: [email protected]
fatfoo is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-22-2009, 02:05 AM   #10
Naughty
Confirmed User
 
Industry Role:
Join Date: Jul 2001
Location: Utopia
Posts: 6,479
Quote:
Originally Posted by AIbenjamink View Post
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?
__________________
seks.ai for sale - ping me
Naughty is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-22-2009, 07:40 AM   #11
AIbenjamink
Confirmed User
 
AIbenjamink's Avatar
 
Industry Role:
Join Date: Jan 2009
Posts: 420
Quote:
Originally Posted by Naughty View Post
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

AIbenjamink is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-22-2009, 10:26 AM   #12
Naughty
Confirmed User
 
Industry Role:
Join Date: Jul 2001
Location: Utopia
Posts: 6,479
Quote:
Originally Posted by AIbenjamink View Post
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
Naughty is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.