7-29-2010 Dynamically Creating Zip Archives with PHP

Never having had the need to create a zip archive manually via PHP before, I was a little worried that it’d be a pain.  Not so!  In fact, it was extremely simple.  I first googled up this tutorial which set up the basics for me and working from there and with the PHP docs online, I was able to complete the project in almost no time at all.

One thing that was a little complicated was cleaning up after I finished.  I didn’t want to remove the zipped files and the archive before I could feel confident that the visitor to the site had downloaded everything they wanted to.  Thus, a separate process had to do the clean-up operation.  I opted to add some code to the index.php file within the same folder as all the created, temporary files.  Then, when someone visited it, that code would clean-up all the extraneous files.

Here’s what I did:

1
2
3
4
5
6
7
8
9
10
11
$now = time();
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("."));
foreach($iterator as $file) {
     if(substr($file, -3, 3) == "php" || $file->getFilename() == "template.docx" || ($now - $file->getCTime())  < 300) continue;
     $temp = explode(DIRECTORY_SEPARATOR, $file);
     $folders[] = $temp[1];
     unlink($file);
}

$folders = array_unique($folders);
foreach($folders as $folder) rmdir("./$folder");

The if-conditional just makes sure that I don’t delete php files, the template word document from which others are generated, or any files that are less than 5 minutes old.  That time constraint makes me feel like anyone who doesn’t get the files they wanted probably didn’t click the Open/Save button in the dialog after making the files and they can just re-do the work.

How did I use the template document to make new ones, you ask?  The PHPWord class, that’s how!

Pagan Newswire Collective

Great meeting tonight of the Pagan Newswire Collective.  Topics ranged from local bureaus to the tech team to how we can work with larger, “mainstream” media outlets.  I’ll give you three guesses regarding how I’ll be assisting the PNC as it moves toward a greater involvement in the larger Pagan community. I’ll update this post [...]

Belief in Question

I just had a wonderful evening spent in discussion with others, mostly students but at least two others who were not, regarding the benevolence of God. The group is called Belief in Question. As far as I can tell, there’s no other web presence for the group, which is unfortunate, as I’d love to be [...]

Activation

I spent a while working on how I wanted to handle plugin activation for Phorum over this weekend.  I investigated using the existing WordPress tables to store the forum information but, in the end, opted to create my own custom ones for one specific reason:  it’ll be easier to clean up after myself if someone [...]

Phorum Begins

So, as you might have noticed, my web site suddenly became all WordPress-ified this past winter.  Since then, I’ve worked on a few other sites in WordPress, too, and in general found it to be a very positive experience.  One of those sites is the site for my World of Warcraft guild, Team Venture. One [...]