Create and email ZIP files on the fly!

I was faced with a peculiar request tonight. One of my hosting clients wanted me to write a script that would automatically email him his Access database each night. Even though our servers are backed up on a daily basis, he wanted a copy sent through to him. After a bit of searching I came across a fantastic CFC created by Nate Nielson, http://www.webclarity.com, which creates ZIP files on the fly. As this guy's database is over 1MB in size I thought, perfect!

There are a couple of things that you'll need before implementing this tutorial though:

  1. The CFC component which is free and can be downloaded from Nate's site here: http://www.webclarity.com/developers/zip.cfm
  2. You need to have the function createObject enabled on your server
  3. You will also need to find about 5 minutes of spare time because that's how long it took me to get this working!


NB: by enabling createObject on a server a possible security implication raises its head in that users could access the Service Factory setting in the CF Adminstrator and view datasources, change passwords etc. For this reason some hosts may NOT allow you to use this function so check with them first!

OK. The CFC component itself is beautiful! Short, simple and quick. Everything a function should be so at this point it's only fair to offer congratulations to Nate for his creation. How you include the component in your page is totally up to you but I simply used <CFINCLUDE> to include it. I won't go into detail as to how the CFC component actually works as Nate covers this in detail on both his site and in the download itself.

Create a new file called email_zip.cfm or whatever and add the code below:

<!---include the zip cfc--->
<cfinclude template="zip.cfc" />

<!---set the path to the file that you want to zip up, obviously change the file path to suit--->
<cfparam name="dbFile" default="c:\domains\mydomain.com\db\myDatabase.mdb" />

<!---I wanted to give the zip file a unique name so all I'm doing here is creating a date stamp for the zip file. This is only one of many ways to do this --->
<cfset zipTS="#DateFormat(Now(), 'ddmmyy')#">

<!---set the path to the zip file you are going to create. Again the actual path will be up to you. The zip file is named and stamped with the date--->
<cfparam name="dbZip" default="c:\domains\mydomain.com\wwwroot\dbBackup-#zipTS#.zip" />

<!---the following script does four simple actions to actually create the zip file --->
<cfscript>
// create an instance of the zip component 
zipper = createObject("component", "zip");

// start a new zip file. This just uses the path we set above 
zipper.newZip("#dbZip#"); 

// add files to zip. Again this uses our file path above. 
zipper.addFile("#dbFile#");

// create the zip file 
zipper.createZip();
</cfscript>

<!---OK we now have our zip file sitting on the server so all we do is attach it to an email and off it goes! --->
<cfmail to="foo@bar.com" from="bar@foo.com" subject="Database Backup for #DateFormat(Now(), ' d mmmm yyyy')#">
    Attached to this email is the latest backup for your database
    <cfmailparam file="#dbZip#" />
</cfmail>

And that's it! All there is to it. The email gets sent out to the recipient with a compressed zip file attached. To add a bit of automation I simple created a scheduled task to run each night to send the email automatically.

Again thanks to Nate for a great CFC and I hope this helps others as much as it helped me. It certainly got a big thumbs up from my client!

Submitted by: Phil Williams
Email: support<at>openmindhosting.com
Web: http://www.openmindhosting.com

All ColdFusion Tutorials By Author: Phil Williams
  • SE Friendly URL's
    Get the search engines to really dig deep into your site by replacing the ? and & in your dynamic URL's with /. Tricks the SE bot into thinking it's a regular folder and eats up your content!
    Author: Phil Williams
    Views: 35,619
    Posted Date: Wednesday, February 12, 2003
  • Currency Conversion using Web Services
    A very simple currency convertor that uses the latest exchange rates through the available Web Service
    Author: Phil Williams
    Views: 24,915
    Posted Date: Wednesday, April 21, 2004
  • Remote Reboots with ColdFusion MX
    This tutorial will tell you how to reboot your server without even logging in.
    Author: Phil Williams
    Views: 15,424
    Posted Date: Sunday, July 18, 2004
  • Create and email ZIP files on the fly!
    This tutorial will allow you to zip up a file or files on your server and email them to you. The whole tutorial runs to less than 20 lines of code!
    Author: Phil Williams
    Views: 18,669
    Posted Date: Thursday, October 28, 2004
  • Stop automated form submission by using Image Verification
    This tutorial will show you how to stop automatred form submissions by generating a random image that must be verified before the form is submitted.
    Author: Phil Williams
    Views: 20,101
    Posted Date: Thursday, October 20, 2005
  • Complete site encryption!
    This tutorial will show you how to encrypt an entire site by recursively working though the folder structure encrypting all the cfm files and copying any non-cfm files whilst keeping the directory structure intact.
    Author: Phil Williams
    Views: 12,219
    Posted Date: Sunday, July 16, 2006
  • Simple and Efficient RecordSet Paging
    A simple and efficient method to page through hundred or even thousands of recordset result.
    Author: Phil Williams
    Views: 5,248
    Posted Date: Thursday, January 8, 2009
  • Backup Your MySQL Database with just ColdFusion Code

    This tutorial will show you how to quickly and easily backup and restore a MySQL database using just ColdFusion scripts and avoiding having to use CFEXECUTE which is normally disallowed on shared hosting platforms.

    ColdFusion 8 has some handy tags that will provide database detail but what if you're running CF7? Normally you would have to resort to running the mysqldump command but this is restrictive if you wanted to provide your users with a method to backup their databases.


    Author: Phil Williams
    Views: 2,954
    Posted Date: Friday, July 10, 2009
Download the EasyCFM.COM Browser Toolbar!