download file zip from subdirectory using php

Solutions on MaxInterview for download file zip from subdirectory using php by the best coders in the world

showing results for - "download file zip from subdirectory using php"
Mira
05 Mar 2016
1<?php 
2// Create ZIP file
3if(isset($_POST['create'])){
4  $zip = new ZipArchive();
5  $filename = "./myzipfile.zip";
6
7  if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
8    exit("cannot open <$filename>\n");
9  }
10
11  $dir = 'includes/';
12
13  // Create zip
14  createZip($zip,$dir);
15
16  $zip->close();
17}
18
19// Create zip
20function createZip($zip,$dir){
21  if (is_dir($dir)){
22
23    if ($dh = opendir($dir)){
24       while (($file = readdir($dh)) !== false){
25 
26         // If file
27         if (is_file($dir.$file)) {
28            if($file != '' && $file != '.' && $file != '..'){
29 
30               $zip->addFile($dir.$file);
31            }
32         }else{
33            // If directory
34            if(is_dir($dir.$file) ){
35
36              if($file != '' && $file != '.' && $file != '..'){
37
38                // Add empty directory
39                $zip->addEmptyDir($dir.$file);
40
41                $folder = $dir.$file.'/';
42 
43                // Read data of the folder
44                createZip($zip,$folder);
45              }
46            }
47 
48         }
49 
50       }
51       closedir($dh);
52     }
53  }
54}
55
56// Download Created Zip file
57if(isset($_POST['download'])){
58 
59  $filename = "myzipfile.zip";
60
61  if (file_exists($filename)) {
62     header('Content-Type: application/zip');
63     header('Content-Disposition: attachment; filename="'.basename($filename).'"');
64     header('Content-Length: ' . filesize($filename));
65
66     flush();
67     readfile($filename);
68     // delete file
69     unlink($filename);
70 
71   }
72}
73?>