Creating a PHP File Archive System

Creating a PHP File Archive System thumbnail
Use PHP to automatically archive your site's content.

You may be one of the many programmers who has chosen PHP as the server-side computer language to drive your input-intensive Internet application. If you run a blog or social networking site, avail yourself of this dynamic language's engagement with archiving technology: create a system that will automatically compress and back up the files that hold your most important content. To do so you can employ one of the most widely used archive types, with the ".zip" extension, to pack chosen data into a single, tidy and compact file.

Things You'll Need

  • Server running PHP version 5.2.0 or higher
Show More

Instructions

    • 1

      Write code that automatically names the archive that you will be creating. The sample below does so by employing PHP's "date()" function:

      <?php

      $newZipFileName="archive_for_" . date("Ymd_H-i-s") . ".zip";

      The filename generated by this code is unique and precise down to the second. Such exactness ensures that archives will not overwrite one another. For example, if the script were executed on January 11th of 2010, at exactly five in the afternoon, the new file name would be "archive_for_20100111_16-00-00.zip".

    • 2

      Create an instance of the ZipArchive class, then use it to create a zip file with the name you established above. For example, you could write the following:

      $zip = new ZipArchive;

      $open = $zip->open($newZipFileName, ZipArchive::CREATE);

    • 3

      Add files to the archive, now that it is open, by using the "addFile()" command. Following is a piece of code that will archive all of the files from a directory called "blogEntries". Include "echo" statements, as this sample does, if you want to see exactly which files are being added to the archive:

      if ($handle = opendir('blogEntries/'))

      {

      echo "Directory handle: $handle\n";

      echo "Files:\n";

      while (false !== ($file = readdir($handle)))

      {

      echo "beStory/$file";

      $zip->addFile("blogEntries/".$file);

      }

      closedir($handle);

      }

    • 4

      Close the file. Now that you have written the code that will add files to the archive, include the "close()" statement:

      $zip->close();

      ?>

    • 5

      Secure your script by adding an "if" statement that will throw an error in case the new file does not create properly. Here is the entire sample script, with such a statement added:

      <?php

      $newZipFileName="archive_for_" . date("Ymd_H-i-s") . ".zip";

      $zip = new ZipArchive;

      $open = $zip->open($newZipFileName, ZipArchive::CREATE);

      if ($open)

      {

      echo "ZipArchive open!";

      if ($handle = opendir('blogEntries/'))

      {

      echo "Directory handle: $handle ";

      echo "Files:";

      while (false !== ($file = readdir($handle)))

      {

      echo "blogEntries/$file";

      $zip->addFile("blogEntries/".$file);

      }

      closedir($handle);

      }

      $zip->close();

      }

      else echo "Zip Archive failed to create and open.";

      ?>

    • 6

      Encapsulate this script as a function, and have your main program call it automatically. Alternatively, place it in a stand-alone PHP file and execute it as frequently as you wish merely by visiting that file in your browser.

Tips & Warnings

  • Alter the precision of the "date" function implemented in the naming of the archive. In some cases, you may want old archives to be automatically overwritten. A file that contains the year and month, but not the day or time, will automatically delete the old version of itself (overwrite it) until the month changes. Also, remember that the dates returned by this function depend upon your server's settings, so format accordingly.

  • Change the file-handling part of the script to include only a certain type of file, if you wish. For example, use "str_pos()" to confirm that ".txt" is held in a file name, and set this as a condition for inclusion in the archive. You may want to automate the name of the directory in a similar fashion, according to where the files to be backed up are stored on your server.

  • The ZipArchive library is not available to PHP versions earlier than 5.2.0. Even in versions to which the library is available, it may not be installed. Talk to your system administrator if the class does not seem to be working for you.

Related Searches:

References

  • Photo Credit stock image of earth in binary code image by Ruslana Stovner from Fotolia.com

Comments

You May Also Like

Related Ads

Featured