PHP Classes

File: ZipFolderTree.php

Recommend this page to a friend!
  Classes of Mat Jung   PHP Zip Folder Tree   ZipFolderTree.php   Download  
File: ZipFolderTree.php
Role: Example script
Content type: text/plain
Description: zips all files in folder with subfolders
Class: PHP Zip Folder Tree
Creates a ZIP archive with all files from a folder
Author: By
Last change:
Date: 2 years ago
Size: 1,269 bytes
 

Contents

Class file image Download
<?php
/**
 * Summary Add all files in all folders and subfolders into a ZipArchive
 *
 * Description. Add all files in all folders and subfolders into a ZipArchive
 *
 * @filename ZipFolderTree.php
 *
 * @package None
 * @subpackage None
 * @since 1.0.0
 * @author Mat Jung, https://www.phpclasses.org/browse/author/869460.html
 * Usage
 * Input: targetPfad: FileName, Name of Output Zip ( zip should not exist )
 * Input: directory: FolderName, Name of folder to be zipped
 *
 * put ZipFolderTree.php into the parent folder
 */
$targetPfad="MediaWiki30.zip";
$directory = new RecursiveDirectoryIterator('MediaWiki30');
$recursiveIterator = new RecursiveIteratorIterator($directory);
if(
file_exists($targetPfad)) { die("Zip Archive Found Exception");}
$zip = new ZipArchive();
$zipArchive = $targetPfad;

if (
$zip->open($zipArchive, ZipArchive::CREATE)!==TRUE) {
    exit(
"cannot open <$filename>\n");
}
foreach(
$recursiveIterator as $info ){
    if (
$info->getFilename() == ".") continue; // ignore . and ..
   
if ($info->getFilename() == "..") continue;
    echo
$info->getPathname() . "<br>\n";
   
$zip->addFile($info->getPathname());
    echo
"Status and Files: " . $zip->status . " " . $zip->numFiles . "<br>\n";
}
$zip->close();
?>