The zip
will be finally written to file AFTER you've called $zip->close()
. Until this point everything happens in memory, no 'zipping' is done. That's why you can delete the unzipped files only after you've called $zip->close()
successfully.
The documentation even says the following:
When a file is set to be added to the archive, PHP will attempt to lock the file and it is only released once the ZIP operation is done. In short, it means you can first delete an added file after the archive is closed.
However, the locks will not prevent you from deleting the files anyway, they are just "hints", the big problem is that the files need to be there for processing on close()
.
So the inner loop should look like this:
foreach ($attachment_files as $attachment) {
$zip->addFile($tempDir.$dir_name."/".$attachment, $dir_name."/".$attachment));
$to_be_unlinked []= $tempDir.$dir_name."/".$attachment;
}
Later on, unlink the files:
...
foreach($to_be_unlinked as $file) {
unlink($file);
}