Search code examples
phplinuxubuntuencodingphp-ziparchive

PHP - Encoding error on add file to a zip with ZipArchive on Linux


I'm having trouble while adding files to a zip when the file name has special characters like "áéãô". The code is really simple and everything is working. Here it is:

$newZip = new ZipArchive();
$newFile = DIR."/".$repo_name.".zip";
$open_newZip = $newZip->open($newFile, ZIPARCHIVE::CREATE);

        if($open_newZip === TRUE){

            sort($arquivos);    

            foreach( $arquivos as $arquivo ){
                //I tried this but it doesn't make any difference at all
                header('Content-Type: text/html; charset=utf-8'); 

                $newZip_baseName = pathinfo($arquivo, PATHINFO_BASENAME);
                $newZip->addFile($arquivo,  $newZip_baseName);
        } else {
            echo 'Error openning the new file. Error: '. $open;
        }

For example: the file is "1Nandõ Reis - N.mp3" and in the zip file it appears like "1Nand├╡ Reis - N.mp3". When unziped (it's correct? hahaha) the file is still "1Nand├╡ Reis - N.mp3".

I tried this: Getting UTF-8 filenames to work with PHP ZipArchive (which gives me a different result - the file is saved like "1Nandї Reis - N.mp3"- but doesn't solve my problem)

And this: http://grokbase.com/t/php/php-bugs/1127mqqbd8/php-bug-bug-53948-new-zip-archive-utf-8-filenames-problem

And the oficial bug report: https://bugs.php.net/bug.php?id=53948

Most of the links above indicates this: PHP ZipArchive non-English filenames return funky filenames within archive which I also tried without any success. I'm afraid that all this solutions are for windows only. Someone have a workaround for Linux?


Solution

  • It is the usual «on my computer I can save a file with that name, so it's your fault» syndrome. Never seen a Justin Bieber's MP3 with a lot of ♥ in the filename "just because it's possible"?

    For Linux I found the command convmv which sounds promising — see http://linux.die.net/man/1/convmv

    I think you can exec() it from PHP, too.