I have the following code:
$zip = new ZipArchive();
$zip->open($nome, ZIPARCHIVE::CREATE);
foreach($files as $file) {
$zip->addFile($file,basename($file));
}
The $files is an array with paths to the actual files, and they're something quite like '../folder/anotherFolder/file1.txt', '../folder/anotherFolder/file2.txt' and so on.
The zip file is created successfully, but, when I unzip it, I have both file1.txt and file2.txt, as expected because of the basename($file) parameter of $zip->addFile(), but also folder/anotherFolder/file1.txt and folder/anotherFolder/file2.txt.
If I take off the second parameter, basename($file), there'll be no duplicated entries on my zip file, but the folder/anotherFolder/ structure will be there, whilst all I wanted was merely, and only, file1.txt and file2.txt.
Any idea why is this happening?
I thought I had the same problem, and it turned out to be because I worked on the same output file and didn't set the ZipArchive::OVERWRITE flag:
In a first attempt, I didn't set the second parameter for addFile(). In a second attempt, I did set it, but as the output zip file already existed from my first attempt, files were only added to the archive, while the original files remained in place in the archive, creating the illusion of a double addition..