我在PHP中使用/usr/bin/zip
创建和下载zip文件。问题是zip文件包含带有非ASCII文件名的csv文件。我下载了零字节文件,文件无效。
chdir($tmp_dir); // this is the directory where the files are written into
// CSV files that will be included in the zip file.
// assuming that the file already exist in $tmp_dir
$files = array();
$filename = "ショップ" . date("Ymd") . ".csv";
$fpath = $tmp_dir. DS . mb_convert_encoding($filename, "SJIS", "UTF-8");
$files[] = $fpath;
// The zip file to be created
$zip_file = "archive_" . date("Ymd").".zip";
$cmd = "/usr/bin/zip $zip_path *.csv";
exec($cmd);
// Force download
$fpath = $zip_file;
header("Content-Type: application/zip");
header('Content-Disposition: attachment; filename="' . $zip_path . '"');
header('Accept-Ranges: bytes');
if ($this->isIE()) {
header("Cache-Control:private");
header("Pragma:private");
}
header('Content-Length: ' . filesize($fpath));
readfile($fpath);
我尝试了ZipArchive,但同样的问题也出现了。
chdir($tmp_dir); // this is the directory where the files are written into
// CSV files that will be included in the zip file.
// assuming that the file already exist in $tmp_dir
$files = array();
$filename = "ショップ" . date("Ymd") . ".csv";
$fpath = $tmp_dir. DS . mb_convert_encoding($filename, "SJIS", "UTF-8");
$files[] = $fpath;
// The zip file to be created
$zip_file = "archive_" . date("Ymd").".zip";
$zip = new ZipArchive();
$zip->open($zip_path, ZipArchive::CREATE);
foreach ($files as $v) {
$zip->addFile(basename($v));
}
$zip->close();
// Force download
$fpath = $zip_file;
header("Content-Type: application/zip");
header('Content-Disposition: attachment; filename="' . $zip_path . '"');
header('Accept-Ranges: bytes');
if ($this->isIE()) {
header("Cache-Control:private");
header("Pragma:private");
}
header('Content-Length: ' . filesize($fpath));
readfile($fpath);
这有什么解决方法吗?当我从文件名中删除日文字符时,没关系。
答案 0 :(得分:0)
我使用函数iconv解决了这个问题,将文件名转换为正确的字符集。
$filename = iconv('SJIS', 'CP392//TRANSLIT', "ショップ" . date("Ymd") . ".csv");
$fpath = $tmp_dir. DS . $filename;
这意味着它将输入字符集SJIS转换为文件名的输出字符集CP392。 CP392是code page的Shift JIS。
代码页932(缩写为CP932,也称为IANA名称 Windows-31J)是微软对Shift JIS的扩展,包括NEC 特殊字符(第13行),NEC选择的IBM扩展(第89行) 到92)和IBM扩展(第115到119行)。编码字符集 是JIS X0201:1997,JIS X0208:1997,以及这些扩展。的Windows-31J 经常被误认为是Shift JIS:虽然相似,但区别在于 对于希望避免使用mojibake的计算机程序员而言非常重要,以及 很好的理由使用明确的UTF-8代替。 windows-31J的名字 然而,这是IANA,并且在历史上并未得到Microsoft的认可 使用了shift_jis。