【发布时间】:2015-01-04 02:15:52
【问题描述】:
我正在 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);
有什么解决方法吗?当我从文件名中删除日文字符时,就可以了。
【问题讨论】:
标签: php linux utf-8 zip non-ascii-characters