Outils pour utilisateurs

Outils du site


php:zip

ZIP

Installation

Installer le paquet php-zip :

sudo apt install php-zip

Zipper tout le contenu d'un dossier

Source: https://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php

// Get real path for our folder
$rootPath = realpath('/home/marc/Bureau');
 
// Initialize archive object
$zip = new ZipArchive();
$zip->open('backup.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
 
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);
 
foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);
 
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}
 
// Zip archive will be created only after closing object
$zip->close();
php/zip.txt · Dernière modification: 2020/05/31 08:10 (modification externe)