I am using php ZipArchive and I have run into a simple problem.
I need to create new file and if there is existing file with that name I need to overwrite it.
My problem is this, if I use this code
if ($zip->open($packageFileName, ZipArchive::OVERWRITE) === TRUE)
it will overwrite any existing file with the same name, however if file with such a name does not exist then nothing happens, if case returns false and I fill the zip file with files inside this if case, but since if file is not on the disk above fails and no file gets created.
If I do this
if ($zip->open($packageFileName, ZipArchive::CREATE) === TRUE)
it will return true and file will be either created or updated, but I do not want to update the file, I need to overwrite the file.
So I am stuck a bit, I want file overwritten and if it does not exist I want it created, is there a way to do this in one line so I do not have to check this in multiple lines where I check if file exists and then pick either CREATE or OVERWRITE, why is there not CREATE_OVERWRITE Constant?
Just like the error codes in PHP the zip flags are also bitwise additive means you can combine them using OR (|
) to do more...
if ($zip->open($packageFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE))
Above will create a zip if it's not there else will overwrite. So this is how you do your CREATE_OVERWRITE
.