How can the root folder be excluded from the zip archive when using PHP ZipArchive?
To exclude the root folder from the zip archive when using PHP ZipArchive, you can add the files directly to the archive without including the root folder itself. This can be achieved by iterating over the files within the root folder and adding them individually to the archive.
$zip = new ZipArchive;
$zipFileName = 'archive.zip';
$zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$rootFolder = 'path/to/root/folder';
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootFolder),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($files as $file) {
$localPath = substr($file, strlen($rootFolder) + 1);
if ($file->isFile()) {
$zip->addFile($file, $localPath);
}
}
$zip->close();
Keywords
Related Questions
- What are some alternative methods to automatically generate directories and index files in PHP, aside from using mkdir and file_put_contents functions?
- What potential issue can arise when using ($_SERVER['PHP_SELF']) in a form tag in PHP?
- How can one ensure that wordwrap does not cut off words in PHP?