There are a number of problems with your zipFolder()
function that makes the .odt
file broken. The file loader used in LibreOffice is not very forgiving, this might also apply to OpenOffice since the former is a fork of the latter.
Thanks to PHP bug report #48763 I've managed to narrow down the problem. This bug report mostly deals with a problem with ZipArchive::addFromString()
, which has been fixed since PHP 5.2.11. However user "Lars" gives an insight to a limitation in the LibreOffice file loader.
"When using windows filesystem separators the .ods zip archive is broken, even though extracting the archive is working."
1. "." and ".." are still included in the archive
You have an if
statment going like this:
if (in_array(substr($file, strrpos($file, '/')+1), array('.', '..'))) {
continue;
}
I don't know if the intention was to filter out the .
and ..
, anyhow it doesn't do the trick. Since you're including ..
, which together with realpath()
translates into the parent directory, you're breaking the .odt
file.
2. All directory separators must be forward slashes (unix style)
On windows, directory separators are of the backward slash type (\
). This explains why your script is working on linux (as tested by user CrazySabbath) but not on windows (XAMPP). As per the bug report I mentioned in the beginning you must use forward slashes (/
) as directory separators for LibreOffice to open your files.
Note also that realpath()
on windows will change unix style paths to windows style.
The ZIP file standard states that all slashes MUST be forward slashes, however it seems that ZipArchive
let's you ignore the standard by not doing the conversion for you.
4.4.17.1 The name of the file, with optional relative path. The path stored MUST not contain a drive or device letter, or a leading slash. All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility with Amiga and UNIX file systems etc.
3. DIRECTORY_SEPARATOR is not necessary
Not a problem with your code, just a general tip. There is no need to use the constant DIRECTORY_SEPARATOR
, simply use forward slashes (/
) and it will work on *nix and windows systems alike.
However, DIRECTORY_SEPARATOR
is still useful for things like exploding or replacing a path.