473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need code to find 2-day old files

Can this be done in PHP?

I want to read a directory and for each file with a .sql extension that is
older than 3 days, I want to delete it.

I have a cron job that makes mysqldump files (xxx.sql) each night and only
want to keep the last 2 or three of them around.

This will also be a cron job (I know how to run a php script in cron.)

I could do this with a rather complex "find" command piped to "rm" but I'd
rather do it in PHP if possible.

Any ideas?

Thanks,

Al

Jul 16 '05 #1
2 3144
sam
"Adams-Blake Co." <at************@adams.takeme.out.-blake.com> wrote in message news:<be**********@slb3.atl.mindspring.net>...
Can this be done in PHP?

I want to read a directory and for each file with a .sql extension that is
older than 3 days, I want to delete it.

I have a cron job that makes mysqldump files (xxx.sql) each night and only
want to keep the last 2 or three of them around.


function delete_old_sql_files($number_of_days)
{
$age = $number_of_days * 60*60*24; // number_of_days in seconds

$dir_path = "path/to/your/directory";
$mydir = dir($path);
while($file_name = dir->read())
{
if(ereg(".sql",$file_name))
{
$file_time = filemtime($dir_path."/".$file_name);
$now_time = time();

if($file_time - $now_time > $age)
delete($dir_path."/".$file_name);
}
}
dir->close();
}

// I didn't test this script.
// I did write it just now (for you)
// Hope this helps.
Jul 16 '05 #2
sam wrote:
"Adams-Blake Co." <at************@adams.takeme.out.-blake.com> wrote in
message news:<be**********@slb3.atl.mindspring.net>...
Can this be done in PHP?

I want to read a directory and for each file with a .sql extension that is
older than 3 days, I want to delete it.

I have a cron job that makes mysqldump files (xxx.sql) each night and only
want to keep the last 2 or three of them around.


function delete_old_sql_files($number_of_days)
{
$age = $number_of_days * 60*60*24; // number_of_days in seconds

$dir_path = "path/to/your/directory";
$mydir = dir($path);
while($file_name = dir->read())
{
if(ereg(".sql",$file_name))
{
$file_time = filemtime($dir_path."/".$file_name);
$now_time = time();

if($file_time - $now_time > $age)
delete($dir_path."/".$file_name);
}
}
dir->close();
}

// I didn't test this script.
// I did write it just now (for you)
// Hope this helps.

THANK YOU! I will give it a try tonight and let you know.

Al

Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ti33m | last post by:
Hi All, I'd like to include a datasheet on my user interface but since I'm starting to run tight on space, I'd like to have a vertically-oriented datasheet (column 1 has labels, column 2 has...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
6
by: news | last post by:
I need some concise, easy to grok examples of what .Net (preferably C#.Net) has over just using old ASP or PHP with JavaScript. See, I'm a PHP guy, and while I started server-side scripting with...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
9
by: SuperNova | last post by:
Hello! I need to mark similar phrases in two different texts, for example to use <btag. Example: text 1: Google Chrome is a browser that combines a minimal design with sophisticated...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.