简体   繁体   English

PHP ZipArchive非英语文件名返回存档中的时髦文件名

[英]PHP ZipArchive non-English filenames return funky filenames within archive

This code works properly to make the ZIP file with the wanted files, except the filenames in the archive, which are not in English (in this case they are Hebrew), have weird characters instead of the proper hebrew letters. 此代码可以正常工作,以使带有所需文件的ZIP文件(存档中的文件名不是英语(在这种情况下为希伯来语))具有奇怪的字符而不是适当的希伯来字母。

<?php
$filesfordown = $_POST['GEMin'];
    if(empty($filesfordown)) 
    {
        echo "No files were seleceted for download.";
    } 
    else 
    {
$zip_name = "RMW." . time() . ".zip";
$zip = new ZipArchive;
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($filesfordown as $filefordown) {
  $zip->addFile($filefordown);
}
$zip->close(); }

header('Content-Type: application/zip');
header("Content-disposition: attachment; filename='$zip_name'");
header('Content-Length: ' . filesize($zip_name));
readfile($zip_name);

ob_flush;
?>

I did some searching around, it seems that iconv, setlocalte, or mb_convert_encoding might help, but whatever I tried didn't work. 我进行了一些搜索,似乎iconv,setlocalte或mb_convert_encoding可能有所帮助,但是我尝试的任何方法均无效。

Any ideas? 有任何想法吗?

PS As a side question, is there a way to not keep directory structure in the zip? PS作为一个附带的问题,有没有办法在zip文件中不保留目录结构?

ETA: An example of the $_post may be www.domain.com/path/שלום_01.mp3 预计$_post时间: $_post的示例可能是www.domain.com/path/שלום_01。mp3

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. 3件事需要改变。

  1. Verify that the actual php file is UTF-8. 验证实际的php文件是否为UTF-8。
  2. setlocale() needs to include the .UTF-8 at the end. setlocale()需要在末尾包含.UTF-8。
  3. ZipArchive does not handle UTF-8 correctly. ZipArchive无法正确处理UTF-8。 Must use CP. 必须使用CP。 Hebrew was CP862. 希伯来语是CP862。 Therefore, use extra option $localname for addFile, and its basically iconv("UTF-8","CODE_PAGE_REF",$localname) 因此,对addFile使用额外的选项$localname及其基本上的iconv("UTF-8","CODE_PAGE_REF",$localname)

它还适用于: iconv("UTF-8", "CP852", $nameFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM