Getting UTF-8 filenames to work with PHP ZipArchive

后端 未结 1 529

I have a file that I want to add to a Zip with PHP, which is encoded in UTF-8. Here is the filename: \'µ漢字ääÖÅ.txt\'.

Now, to get this file even to save to the files

相关标签:
1条回答
  • 2020-12-21 13:12

    I have found an answer (sort of). In the example above $encoded_filename was changed from UTF-8 to Windows-1252 encoding to save to the filesystem. I have no idea why but Windows-1252 works when saving directly to the filesystem but NOT when saving to a zip using ZipArchive.

    To fix this I had to encode the $filename yet again to a different encoding, CP858.

    Example:

    $filename = "µ漢字ääÖÅ.txt";
    
    //encode to windows-1252 to save to the filesystem
    $encoded_filename = iconv("UTF-8","Windows-1252//IGNORE",$filename);
    file_put_contents($encoded_filename, "text");
    
    //put in a zip file
    $zip = new \ZipArchive();
    $zip->open('test.zip', \ZIPARCHIVE::CREATE);
    //encode as CP858 to save into zip file
    $zip->addFile($encoded_filename, iconv("UTF-8", "CP858//IGNORE", $filename));
    $zip->close();
    

    In the above example it still doesn't handle the Japanese characters in the file name, but at least it handles European characters which will have to do for the time being.

    0 讨论(0)
提交回复
热议问题