Posts Tagged ‘script’

Cache Cleanup Script - Delete Old Files

Friday, February 15th, 2008

The Cache Cleanup Script is designed to do just what it says - clean up temporary files created in some sort of cache directory. This script is meant to accompany some other kind of script that temporarily stores information in a file.

How Cache Cleanup Works

Here’s the source code in text format.

At the top of the script, you need to set two values - $dirName and $timeLimit.

$dirName should be a local path to the directory you want to scan for old files. $timeLimit should be the maximum allowable inactive time, in seconds.

The logic of the script is very straightforward. First, we scandir() to get an array of filenames in our cache directory.

Then, we loop through each fileName. If the file was last accessed before our timeLimit, we delete the file. If not, we leave the file alone.

If the script attempts to delete a file, it will output a success or failure message. Otherwise, there is no output.

How to Implement Cache Cleanup

First, set the configuration variables to your desired settings. I intentionally refrained from using any kind of input into the script for security reasons.

Next, decide how you want to run the script.

If your host allows you to run cron jobs, the logical choice would be to make this a cron job run every so often. Depending on how long you want the temporary files to remain on your server, I’d run the cron job once a day or so.

If your host doesn’t allow cron jobs, you could manually access the script each time you want to check for idle files and delete them. This isn’t a bad choice if you want to check once a week or so, but it is kind of annoying to do it every day.

You could also incorporate this into your caching script. Every time the script creates or accesses a cached file, it could call this script (as a function) and delete unused files. This may not be a desirable option for performance reasons if you have a lot of cached files, but it wouldn’t be bad for smaller cache directories.

Anyhow, I just wrote the script. You figure out how you want to implement it.

$dirName = 'cache';	// Path to the cache directory
$timeLimit = 300;	// Max time since a file has been accessed
			//   before it should be deleted
 
if (is_dir($dirName)) {
  $files = scandir($dirName);
}
 
foreach ($files as $file) {
  if ($file == '.' || $file == '..') {
    continue;
  }
 
  if (fileatime($dirName . '/' . $file) < time() - $timeLimit) {
    if (unlink($dirName . '/' . $file)) {
      echo "$file deleted successfully.";
    } else {
      echo "$file not deleted.";
    }
    echo '<br />';
  }
}

New Script: PHP Length Conversion Class

Sunday, February 10th, 2008

I was looking for a project to tackle today, and I decided to make a script that would convert lengths from one unit or system to another.

It doesn’t sound all that complicated - and it’s not. I had a working script in a few minutes, but I didn’t like it. It had too many functions to do conversions for individual units. I wanted this script to be pretty well abstracted so that I could easily add new unit types - and so that the source code for the class would be very short.
(more…)

How to Create Multi-Page Forms in PHP, Revisited

Friday, February 8th, 2008

A couple weeks ago, I wrote a short article about how to create a multi-page form.

The simple solution I suggested involved a foreach loop that cycled through the $_POST array and sent every value along in a hidden input element. After a bit of reflection (and some useful comments), I realized there’s a teeny tiny security hole in that approach - so I’ve slightly modified it to close the loophole.
(more…)

How to Send E-Mail in PHP: A “Share This” Form

Wednesday, February 6th, 2008

This question comes up a lot in forums - how do you send e-mail in PHP?

There are plenty of reasons you’d want to do this. Perhaps you want to send a newsletter to your users. Or you want people to be able to leave feedback through e-mail. Or you just want your users to be able to e-mail the page (or a link to it) to a friend.

How do we do this? The mail() function.
(more…)