PHP ZipArchive non-English filenames return funky filenames within archive

懵懂的女人 提交于 2019-11-29 05:23:25
Daniel

Yay! Fixed!

First the code, then an explanation:

<?php 
setlocale(LC_ALL, 'he_IL.UTF-8');
$filesfordown = $_POST['GEMin'];
    if(empty($filesfordown)) 
    {
        echo "לא נבחרו.. נסה שוב";
    } 
    else 
    {
$zip_name = "RMW" . time() . ".zip";
$zip = new ZipArchive;
$zip->open($zip_name, ZipArchive::CREATE);
echo "מכין את ההורדה...";
foreach ($filesfordown as $filefordown) {
  $zip->addFile($filefordown, iconv("UTF-8","CP862",basename($filefordown)));
}
$zip->close(); 

3 things needed to be changed.

  1. Verify that the actual php file is UTF-8.
  2. setlocale() needs to include the .UTF-8 at the end.
  3. ZipArchive does not handle UTF-8 correctly. Must use CP. Hebrew was CP862. Therefore, use extra option $localname for addFile, and its basically iconv("UTF-8","CODE_PAGE_REF",$localname)

It also works with: iconv("UTF-8", "CP852", $nameFile);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!