Skip to content

Instantly share code, notes, and snippets.

@real34
Created March 17, 2014 17:33
Show Gist options
  • Save real34/9604201 to your computer and use it in GitHub Desktop.
Save real34/9604201 to your computer and use it in GitHub Desktop.
<?php
function fixFilenameEncoding($filename) {
return mb_convert_encoding($filename, 'ISO-8859-2', 'UTF-8');
}
$zip = new ZipArchive;
$res = $zip->open('./elephant.zip', ZipArchive::CREATE);
if ($res === true) {
$filename = 'éléphant-man2.txt';
$zip->addFromString(fixFilenameEncoding($filename), 'file content goes here');
$zip->close();
echo '<a href="elephant.zip">ok</a>';
} else {
echo 'failed';
}
@real34
Copy link
Author

real34 commented Mar 17, 2014

The probleme with this code, is that when opening / extracting the archive with a Zip client the "éléphant-man2.txt" file has an incorrectly encoded name "éléphant-man.txt".

Without the fixFilenameEncoding() the archive content is broken on Ubuntu/OSX and Windows.
With the fixFilenameEncoding() call, it works on Ubuntu but not on the other OSes.

Any idea about how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment