create_zip_archive.php

<?php
#########################################
#
#  OOP version of webzipper index.php
#
# Athor: Paul McNally (enquiries@paulmcnally.info)

#  Date : 03/11/07
#
#########################################
include('includes/udfunctions.php');


                                       


class 
CreateZipArchive {

  public 
$archives_directory;

  
  public function 
__construct($archives_directory){
    
$this->archives_directory $archives_directory;
  }
  



  
// create a 10 digit random number and use that as the archive file name

   
protected function generate_filename(){

         
$rand_filename NULL;

               for (
$i 0$i 10$i++){

                      
$rand_num rand(0,9);

                      
$rand_filename .= $rand_num;
               }

         
$rand_filename .= '.zip';

   return 
$rand_filename;

   }





  public function 
create_zip($file,$archive_name)
  {
    
// create object
    
$zip = new ZipArchive();

          
// open archive or create if doesnt exist
          
if ($zip->open($this->archives_directory.'/'.$archive_nameZIPARCHIVE::CREATE) !== TRUE){
                die (
"Could not open archive");
          }

    
// add the file to the archive
    
$zip->addFile($file) or die ("ERROR: Could not add file: $file");

      
// close and save archive
    
$zip->close();

    
// remove the original file
    
unlink($file);

  }



  public function 
get_file_size($archive_name){
    
    
// read the file size in bytes and convert to MB
    
$filesize number_format((filesize($this->archives_directory.'/'.$archive_name)/1048576),2);
    
    return 
$filesize;
  }




public function 
get_archive_details($archive_name)
  {

     
// scan the zip directory and output it's contents
     
$zip      = new ZipArchive();

     
$filename $this->archives_directory.'/'.$archive_name;

                  if (
$zip->open($filename)!==TRUE) {
                     exit(
"cannot open <$filename>\n");
                  }


                  
$details = array();

                        for (
$i=0$i<$zip->numFiles;$i++) {

                            
$array $zip->statIndex($i);

                            
$details[$i]['file_name']        = $array['name'];
                            
$details[$i]['file_size']           = $array['size'];
                            
$details[$i]['comp_size']       = $array['comp_size'];
                            
$details[$i]['comp_method'] = $array['comp_method'];

                                     }

                     
//print_r($details);

     
$zip->close();

     return 
$details;


  }  
// end func







  
public function download_zip($archive_name){

    echo 
"<div align=\"center\">
    <a href=\"{$this->archives_directory}/$archive_name\"><img src=\"download.png\" width=\"121\" height=\"73\" alt=\"Download\" title=\"Download\" id=\"download\" /></a><br /><br />
    </div>"
;;
}



// end class


?>





<?php




class WebzipperZipCreator extends CreateZipArchive{




public function 
show_upload_form()
  {
    echo 
'<div id="upload_form_div" align="center"><form action="index.php" method="post" enctype="multipart/form-data">
          <input type="file" name="archive" id="file" />
          <input type="submit" name="submit" value="Add to Archive"  onclick="return validate();" />
         '
;

         if (!isset(
$_POST['archive_name'])){

           echo 
'<input type="hidden" name="archive_name" value="'.$this->generate_filename().'" />';

           } else {

             echo 
'<input type="hidden" name="archive_name" value="'.$_POST['archive_name'].'" />';

           }

    echo 
'</form></div>';
   }



  public function 
show_archive_details($archive_name)
  {

    echo 
"<div align='center'>Total Zip File Size: ".$this->get_file_size($archive_name)." MB </div>";

     echo 
"<br />";
     echo 
"<strong>Your archive contains the following files</strong><br /><br />";

    
     
$details $this->get_archive_details($archive_name);
     
   
//print_r($details);

     
foreach ($details as $item){

      echo 
'File Name: '.$item['file_name']."<br />";
      echo 
'File Size :'.$item['file_size']." bytes<br />";
      echo 
'Size on Computer : '.$item['comp_size']." bytes<br />";
      echo 
'Compression Method :'.$item['comp_method']."<br />";

       echo 
"<span class='complete'>Added Successfully</span><br /><br />";
     }





  }  
// end func







// end class






?>





































<?php

// MAIN
$webzipper = new WebzipperZipCreator('archives');


if ((isset(
$_POST['submit']) && $_POST['submit'] == "Add to Archive") && 
    (isset(
$_POST['archive_name']) && is_numeric(substr($_POST['archive_name'], 0, -4))))
{
  if (
is_uploaded_file($_FILES['archive']['tmp_name']))
  {
      
move_uploaded_file($_FILES['archive']['tmp_name'], $_FILES['archive']['name']);
      
      
$file =  $_FILES['archive']['name'];
      

      
$webzipper->show_upload_form();
      
$webzipper->create_zip($file,$_POST['archive_name']);
      
$webzipper->download_zip($_POST['archive_name']);

      
$webzipper->show_archive_details($_POST['archive_name']);

   
   
         
//log the action
      
$log fopen('log.txt',"a");
      
date_default_timezone_set('Europe/London');
      
//@fwrite($log,date('h:i:s d-m-Y ')." Item '$file' added to zip archive '".$_POST['archive_name']."' by {$_SERVER['REMOTE_ADDR']}\n");
      
@fwrite($log,date('h:i:s,d-m-Y').",Item '$file' added to zip archive '".$_POST['archive_name']."',{$_SERVER['REMOTE_ADDR']}\n");
      
fclose($log);


  }
} else {
    
    
remove_old_files('archives');

    
$webzipper->show_upload_form();
}


?>

Paul's Code Library

Categories

Home

AJAX

classes

database_functions

date_functions

file_functions

googlevideo_and_youtube_api

htaccess

image_functions

string_functions