HRDIODB
[ class tree: HRDIODB ] [ index: HRDIODB ] [ all elements ]

Source for file archive.php

Documentation is available at archive.php

  1. <?php
  2. /**
  3.  * Command line file archiving script.
  4.  * This is a command line script which reads information from the HRDI_Downloads
  5.  * table and compresses the necessary files into an archive. The archive is then
  6.  * moved to a web accessible directory, where the polling script notices it and
  7.  * makes it available for download. The PclZip library is used for the Zip file
  8.  * creation. The library page is located at:
  9.  * http://www.phpconcept.net/pclzip/
  10.  * @package HRDIODB
  11.  */
  12.  
  13. /**
  14.  * This file is part of HRDIODB.
  15.  *
  16.  * HRDIODB is free software; you can redistribute it and/or modify
  17.  * it under the terms of the GNU General Public License as published by
  18.  * the Free Software Foundation; either version 2 of the License, or
  19.  * (at your option) any later version.
  20.  *
  21.  * HRDIODB is distributed in the hope that it will be useful,
  22.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  * GNU General Public License for more details.
  25.  *
  26.  * You should have received a copy of the GNU General Public License
  27.  * along with HRDIODB; if not, write to the Free Software
  28.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29.  */
  30.  
  31. /** Configuration file */
  32. require_once("conf.php");
  33. /** HRDI database class */
  34. require_once("hrdi_db_class.php");
  35. /** Download class */
  36. require_once("download_class.php");
  37. /** PclZip library, http://www.phpconcept.net/pclzip/ */
  38. require_once ("pclzip/pclzip.lib.php");
  39.  
  40. // Create class instances
  41. $hdb new hrdi_db ($dsn);
  42. $download new download($hdb);
  43.  
  44. // Check the number of arguments. One argument is allowed.
  45. if ($argc != 2{
  46.   print "Error: This script takes one argument\n";
  47.   exit(1);
  48. }
  49.  
  50. // Get the download id
  51. $id $argv[1];
  52.  
  53. // Make sure that the download id is valid
  54. if (!is_numeric($id)) {
  55.   print "Error: Download id must be a number!\n";
  56.   exit(1);
  57. }
  58.  
  59. // Set time limit to 0 so that the script never times out
  60.  
  61. // Set the memory limit so that we don't pass it
  62. ini_set('memory_limit'($max_archive_size 1)."M")
  63.  
  64. // Get file name and the list of files to compress
  65. list ($file$files$download->get_download_info($id);
  66.  
  67. $error_file "$temp_prefix/$file.error";
  68.  
  69. // Figure out the temporary and destination file locations
  70. $tempfile "$temp_prefix/$file";
  71. $destfile "$archive_prefix/$file";
  72.  
  73. /* Create a new PclZip class instance. This is the representation of the Zip
  74.  * file. */
  75. $archive new PclZip($tempfile);
  76.  
  77. // Create the actual Zip archive
  78. $status $archive->create($filesPCLZIP_OPT_REMOVE_ALL_PATHPCLZIP_OPT_ADD_PATH'hrdifiles/');
  79.  
  80. // Check the status of the archive
  81. if (!$status{
  82.   write_error_file($error_file"Archive creation failed: ".$archive->errorInfo());
  83.   // If archive is not valid, delete it
  84.     unlink($tempfile);
  85.   exit(1);
  86. }
  87.  
  88. // Move the file to its final, web-accessible destination
  89. if (!rename($tempfile$destfile)) {
  90.   write_error_file($error_file"Archive script was unable to move the file to its destination.");
  91.   // If we couldn't move the archive, delete it
  92.     unlink($tempfile);
  93.   exit(1);
  94. }
  95.  
  96. /**
  97.  * Writes an error to a file.
  98.  * This function does what {@link PHP_MANUAL#file_put_contents}
  99.  * does in PHP 5. It takes a file name, opens it in append mode, writes the
  100.  * message and closes the file.
  101.  * @param string $error_file The error file name
  102.  * @param string $message The error message to write
  103.  */
  104. function write_error_file ($error_file$message{
  105.   $fh fopen($error_file'a');
  106.   fwrite($fh$message);
  107.   fclose($fh);
  108. }
  109. ?>

Documentation generated on Tue, 23 Jan 2007 22:57:42 -0500 by phpDocumentor 1.3.0RC6