Most likely, you know that checking through all files each time your CGI script is run will be quite slow and you also know that you will most likely run into access permission problems, as your webserver runs under another user than you are and that user might not have the right to delete files. A cron job would be the cleanest solution in my opinion, but you don't want a cron job.

Perl has some nice "functions" for checking file times, namely -M (age of file in days), -A (last access to file in days) and -C (change to inode).

To use them in your code at startup, I would do more or less something like the (untested) following :

use strict; use File::Find; my @files; # First we fill @files with the (full) path of # all files in the directory find( sub { push @files, $File::Find::name }, "/usr/home/foo/files/" ) +; # Now we check each file if it has become too old foreach (@files) { # If our file is older than one day, we will (try to) delete it if (-M > 1) { unlink $_; # no error checking here ... }; }; ... The rest of your script goes here ...


In reply to Re: unlinking old files by Corion
in thread unlinking old files by damian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.