php


Creating zip file containing the files with UTF-8 encoding name in PHP using /usr/bin/zip


I'm creating and downloading a zip file using /usr/bin/zip in PHP. The problem is that the zip file contains the csv files with the non-ASCII file names. I got the zero-byte file downloaded and the file is not valid.
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);
I tried ZipArchive, but same problem occurs.
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);
Is there any workaround for this? When I removed the Japanese characters from the file name, it is ok.
I solved this problem using a function iconv to convert the file names to the proper charset.
$filename = iconv('SJIS', 'CP392//TRANSLIT', "ショップ" . date("Ymd") . ".csv");
$fpath = $tmp_dir. DS . $filename;
This means that it converts the input charset SJIS to the output charset CP392 for the file name. CP392 is a code page for Shift JIS.
Code page 932 (abbreviated as CP932, also known by the IANA name
Windows-31J) is Microsoft's extension of Shift JIS to include NEC
special characters (Row 13), NEC selection of IBM extensions (Rows 89
to 92), and IBM extensions (Rows 115 to 119). The coded character sets
are JIS X0201:1997, JIS X0208:1997, and these extensions. Windows-31J
is often mistaken for Shift JIS: while similar, the distinction is
significant for computer programmers wishing to avoid mojibake, and a
good reason to use the unambiguous UTF-8 instead. The windows-31J name
however is IANA's and not recognized by Microsoft, which historically
has used shift_jis instead.

Related Links

Form won't validate Username and Password
Multiple tablets to show in a foreach
How to generate HTML table with nested loop?
Echo part of an array based on key => value match
How to solve Reloading Issue in Wordpress?
Subtracting next date value from the current date [closed]
How to use $routeProvider with $_GET Parameters
Saving data from textfile to PostGreSQL database using php
Mysql Comparing dates stored to today's date
Technology behind PDFBuddy [dot] com?
PHP - Convert strings to key value array
How to get seo friendly article url in joomla without assiging it to any menu
How to make a picture responsive that is on wordpress custom post type through HTML/CSS
Laravel 5 get command response
Silently fails when truly prepared, works when emulated
Form submitting duplicate comments to mysql table

Categories

HOME
artificial-intelligence
weblogic12c
yahoo-weather-api
android-fragments
opencv-python
redmine
key
informatica
semantic-web
repair
selection
wine
parsley.js
erd
mpu6050
jodatime
xbox360
async-await
user-defined-types
exec-maven-plugin
cpu-usage
scheduled-tasks
device
web-config-transform
uri
math.js
progid
actframework
cloudera-manager
buildforge
des
netstat
google-prediction
dbcc
jqgrid-asp.net
jenkins-slave
common-table-expression
windows-media-player
embedded-v8
android-mediarecorder
test-data
ptvs
network-protocols
1010
polymaps
gapi
e
logical
jpda
jsf-2.3
p6spy
gcloud-node
qtcpsocket
wolfram-language
rails-engines
facebook-ios-sdk
xcopy
verisign
gridbaglayout
gwt-syncproxy
trust
ioctl
json-spirit
miniconda
highest
dnx50
knife
android-handler
unidata
dbmigrate
physicsjs
wicked-gem
scala-macros
quercus
android-audiomanager
findersync
relationships
quantify
loginview
prism.js
neo4jphp
reference-counting
boolean-algebra
hyphen
vlab
mp4parser
form-for
ax
multiple-dispatch
jquery-dialog
coordinate
zotonic
kobold2d
fitch-proofs
nsfont
kohana-auth
3des
hungarian-notation
ios-4.2
odac
sscli
getimagesize
regioninfo
downloadfile
webclient.uploaddata

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile