Need code to find 2-day old files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adams-Blake Co.

    #1

    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



  • sam

    #2
    Re: Need code to find 2-day old files

    "Adams-Blake Co." <atakeoutcanton @adams.takeme.o ut.-blake.com> wrote in message news:<beb084$ip c$1@slb3.atl.mi ndspring.net>.. .[color=blue]
    > 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.
    >[/color]

    function delete_old_sql_ files($number_o f_days)
    {
    $age = $number_of_days * 60*60*24; // number_of_days in seconds

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

    if($file_time - $now_time > $age)
    delete($dir_pat h."/".$file_nam e);
    }
    }
    dir->close();
    }

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

    Comment

    • Adams-Blake Co.

      #3
      Re: Need code to find 2-day old files

      sam wrote:
      [color=blue]
      > "Adams-Blake Co." <atakeoutcanton @adams.takeme.o ut.-blake.com> wrote in
      > message news:<beb084$ip c$1@slb3.atl.mi ndspring.net>.. .[color=green]
      >> 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.
      >>[/color]
      >
      > function delete_old_sql_ files($number_o f_days)
      > {
      > $age = $number_of_days * 60*60*24; // number_of_days in seconds
      >
      > $dir_path = "path/to/your/directory";
      > $mydir = dir($path);
      > while($file_nam e = dir->read())
      > {
      > if(ereg(".sql", $file_name))
      > {
      > $file_time = filemtime($dir_ path."/".$file_nam e);
      > $now_time = time();
      >
      > if($file_time - $now_time > $age)
      > delete($dir_pat h."/".$file_nam e);
      > }
      > }
      > dir->close();
      > }
      >
      > // I didn't test this script.
      > // I did write it just now (for you)
      > // Hope this helps.[/color]


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

      Al

      Comment

      Working...