PHP ZipArchive非英语文件名返回存档中的时髦文件名 [英] PHP ZipArchive non-English filenames return funky filenames within archive

查看:97
本文介绍了PHP ZipArchive非英语文件名返回存档中的时髦文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码可以正常工作,以使带有所需文件的ZIP文件(归档文件中的文件名不是英语(在本例中为希伯来语))具有奇怪的字符而不是适当的希伯来字母.

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.

<?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;
?>

我进行了一些搜索,似乎iconv,setlocalte或mb_convert_encoding可能会有所帮助,但我尝试执行的任何操作均无效.

I did some searching around, it seems that iconv, setlocalte, or mb_convert_encoding might help, but whatever I tried didn't work.

有什么想法吗?

P.S.附带的问题是,有没有办法在zip文件中不保留目录结构?

P.S. As a side question, is there a way to not keep directory structure in the zip?

ETA: $_post的示例可能是www.domain.com/path/שלום_01.mp3

ETA: An example of the $_post may be www.domain.com/path/שלום_01.mp3

推荐答案

是的!固定!

首先是代码,然后是说明:

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件事.

  1. 验证实际的php文件是否为UTF-8.
  2. setlocale()需要在末尾包含.UTF-8.
  3. ZipArchive无法正确处理UTF-8.必须使用CP.希伯来语是CP862.因此,对addFile使用额外的选项$localname及其基本上为iconv("UTF-8","CODE_PAGE_REF",$localname)
  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)

这篇关于PHP ZipArchive非英语文件名返回存档中的时髦文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆