The following returns a single error, every time: #6, at the end.
Each file exists, it's readable, ZipArchive::addFile returns true, $zipArchive close()s without fail, and yet the final zip file isn't created.
What gives?
** Running this on Windows Server 2012 R2 as a local SAN, if that matters.
Code: Select all
//exceptions, throws ErrorException
set_error_handler('exception_error');
try {
// Copy directory to temporary location
rcopy($src_location,$tmp_location);
// Initialize archive object
$zipArchive = new ZipArchive();
$zipArchive->open($archive_filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($tmp_location),
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($tmp_location) + 1);
if (!file_exists($filePath)) {
_log(1,'Uploading Archives [Compression Error #1]','File Not Found ['.$filePath.']');
throw new Exception('Giving Up');
}
elseif (!is_readable($filePath)) {
_log(1,'Uploading Archives [Compression Error #2]','File Not Readable ['.$filePath.']');
throw new Exception('Giving Up');
}
else {
// Add current file to archive
if (!$zipArchive->addFile($filePath, $relativePath)) {
_log(1,'Uploading Archives [Compression Error #7]','File Not Added ['.$filePath.' | '.$relativePath.']');
throw new Exception('Giving Up');
}
}
}
}
// Zip archive will be created only after closing object
if (!$zipArchive->close()) {
_log(1,'Uploading Archives [Compression Error #3]','Zip Not Created ['.$zipArchive->getStatusString().']');
}
}
catch (ErrorException $e) {
_log(1,'Uploading Archives [Copy/Compression Error #4]',(string)$e);
}
catch (Exception $e) {
_log(1,'Uploading Archives [Copy/Compression Error #5]',(string)$e);
}
finally {
// Clean Up Memory
unset($files);
unset($zipArchive);
}
// Final Check
if (!file_exists($archive_filename)) {
_log(1,'Uploading Archives [Compression Error #6]','Error Creating Archive ['.$archive_filename.'] ('.$src_location.')');
}