The code below successfully creates a zip file of a specified directory and all sub-directories. The problem I am having is that the end result removes the first character from each filename and folder-name within the root directory; this same behavior does not occur in the sub-directories.
<?php
require('../config/config.php');
$rootPath = $abs_path.'/includes/qrcodes/'; // SOURCE FOLDER TO ZIP
$zipFilename = 'qr_images.zip';
$zip = new ZipArchive();
$zip->open($zipFilename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
if (!$file->isDir())
{
$zip->addFile($filePath, $relativePath);
}else {
if($relativePath !== false)
$zip->addEmptyDir($relativePath);
}
}
$zip->close();
?>
Currently the resulting zip file includes:
- adios (folder)
- radio_1.png
- radio_2.png
- nfosys (folder)
- infosys_1.png
- ehicles (folder)
- vehicle_1.png
- vehicle_2.png
The folders should be named "radios", "infosys", and "vehicles".