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 ...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|