PHP Classes

PHP Zip Folder Tree: Creates a ZIP archive with all files from a folder

Recommend this page to a friend!
  Info   View files Example   View files View files (1)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 71 All time: 10,169 This week: 106Up
Version License PHP version Categories
zipfoldertree 1.0Free For Educatio...7PHP 5, Compression, Global
Description 

Author

This package creates a ZIP archive with all files from a folder.

It provides an example script showing how to create a ZIP archive file from files in a folder and any sub-folders the folder may contain.

Usually, PHP installations provide the ZipArchive class with a method to add a globe or add pattern to add multiple files into a zip. In some cases, the add globe is not always working.

This example script uses the ZipArchive, RecursiveDirectoryIterator, and RecursiveIteratorIterator for adding all files in all folders into a zip.

The result is compatible with Windows Explorer Zip.

Picture of Mat Jung
  Performance   Level  
Name: Mat Jung <contact>
Classes: 4 packages by
Country: Switzerland Switzerland
Age: 47
All time rank: 389631 in Switzerland Switzerland
Week rank: 398 Up6 in Switzerland Switzerland Up
Innovation award
Innovation award
Nominee: 2x

Example

<?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();
?>


  Files folder image Files  
File Role Description
Accessible without login Plain text file ZipFolderTree.php Example zips all files in folder with subfolders

 Version Control Unique User Downloads Download Rankings  
 0%
Total:71
This week:0
All time:10,169
This week:106Up