名前が日本語(全角)のファイルをダウンロードする方法
1.日本語名称を直接渡す方法
サンプル(new_page_40.php)の内容
<?php
session_start();
header(“Content-type: text/html; charset=UTF-8”);
if(isset($_GET[“file”])){
$real_fname = $_GET[“file”];
} else {
die(“Error: ファイルが指定されていません”);
}
$_GET = array();
$real_path = “doc/” . $real_fname;
/* ファイルの存在確認 */
if (!file_exists($real_path)) {
die(“Error: File(“.$real_path.“) does not exist”);
}
/* オープンできるか確認 */
if (!($fp = fopen($real_path, “r”))) {
die(“Error: Cannot open the file(“.$real_path.“)”);
}
fclose($fp);
/* ファイルサイズの確認 */
if (($content_length = filesize($real_path)) == 0) {
die(“Error: File size is 0.(“.$real_path.“)”);
}
$fp = fopen($real_path, “rb”);
header(“Content-Length:” . filesize($real_path));
header(“Content-Type:” . mime_content_type($real_path));
header(“Content-Disposition: attachment; filename=’.$real_fname”);
while(ob_get_level()){
ob_end_clean();
}
fpassthru($fp)
?>
日本語名称が【SHIFT-JIS】の場合には
事前に対象ファイル名を【UTF-8】に文字変換しておく
一番、手軽で分かりやすい仕組み
文字変換の手間が難点になる
英数字ファイル名にして中身は画像かPDFにするのが一番簡単
ダウンロードファイルの日本語名を直接渡す
使用例(new_page_40.php)
1
http://●●●●●●/●●●●●●/new_page_40.php?file=日本語名称.txt
2.ファイル名を2種類指定する方法
サンプル(new_page_20.php)の内容
<?php
session_start();
header(“Content-type: text/html; charset=UTF-8”);
$download_fnane = isset($_SERVER[‘PATH_INFO’])? trim($_SERVER[‘PATH_INFO’],‘/’):NULL;
if(isset($_GET[“file”])){
$real_fname = $_GET[“file”];
} else {
die(“Error: ファイルが指定されていません”);
}
$_GET = array();
$atai = explode(“,”,$real_fname);
$real_path = “doc/” . $atai[1];
/* ファイルの存在確認 */
if (!file_exists($real_path)) {
die(“Error: File(“.$real_path.“) does not exist”);
}
/* オープンできるか確認 */
if (!($fp = fopen($real_path, “r”))) {
die(“Error: Cannot open the file(“.$real_path.“)”);
}
fclose($fp);
/* ファイルサイズの確認 */
if (($content_length = filesize($real_path)) == 0) {
die(“Error: File size is 0.(“.$real_path.“)”);
}
$fp = fopen($real_path, “rb”);
header(“Content-Length:” . filesize($real_path));
header(“Content-Type:” . mime_content_type($real_path));
header(“Content-Disposition: attachment”);
while(ob_get_level()){
ob_end_clean();
}
fpassthru($fp)
?>
ダウンロードファイル名と出力ファイル名を日本語で渡す
ダウンロード対象ファイルは【SHIFT-JIS】で英数字
ダウンドード後のファイル名に日本語名称を指定する
使用例(new_page_20.php)
http://●●●●●●/●●●●●●/new_page_20.php?file=日本語名称.txt,aaaaaa.txt
出力ファイル名を指定しないと、省略値解釈で「実行ファイル」となる
3.プログラム内で出力名を明示する方法
サンプル(new_page_30.php)の内容
<?php
session_start();
header(“Content-type: text/html; charset=UTF-8”);
if(isset($_GET[“file”])){
$real_fname = $_GET[“file”];
} else {
die(“Error: ファイルが指定されていません”);
}
$_GET = array();
$real_path = “doc/” . $real_fname;
/* ファイルの存在確認 */
if (!file_exists($real_path)) {
die(“Error: File(“.$real_path.“) does not exist”);
}
/* オープンできるか確認 */
if (!($fp = fopen($real_path, “r”))) {
die(“Error: Cannot open the file(“.$real_path.“)”);
}
fclose($fp);
/* ファイルサイズの確認 */
if (($content_length = filesize($real_path)) == 0) {
die(“Error: File size is 0.(“.$real_path.“)”);
}
$fp = fopen($real_path, “rb”);
header(“Content-Length:” . filesize($real_path));
header(“Content-Type:” . mime_content_type($real_path));
header(“Content-Disposition: attachment; filename=’.$real_fname”);
while(ob_get_level()){
ob_end_clean();
}
fpassthru($fp)
?>
ダウンロードファイル名と出力ファイル名を日本語で渡す、所は同じ
渡す内容は変更せず、プログラム内で出力ファイル名を指定する
【Content-Disposition: attachment】を使って出力する
使用例(new_page_30.php)
http://●●●●●●/●●●●●●/new_page_30.php?file=日本語名称.txt,aaaaaa.txt
4.日本語名称を【PATH_INFO】で渡す方法
サンプル(new_page_10.php)の内容
<?php
session_start();
header(“Content-type: text/html; charset=UTF-8”);
$download_fnane = isset($_SERVER[‘PATH_INFO’])? trim($_SERVER[‘PATH_INFO’],‘/’):NULL;
if(isset($_GET[“file”])){
$real_fname = $_GET[“file”];
} else {
die(“Error: ファイルが指定されていません”);
}
$_GET = array();
$real_path = “doc/” . $real_fname;
/* ファイルの存在確認 */
if (!file_exists($real_path)) {
die(“Error: File(“.$real_path.“) does not exist”);
}
/* オープンできるか確認 */
if (!($fp = fopen($real_path, “r”))) {
die(“Error: Cannot open the file(“.$real_path.“)”);
}
fclose($fp);
/* ファイルサイズの確認 */
if (($content_length = filesize($real_path)) == 0) {
die(“Error: File size is 0.(“.$real_path.“)”);
}
$fp = fopen($real_path, “rb”);
header(“Content-Length:” . filesize($real_path));
header(“Content-Type:” . mime_content_type($real_path));
header(“Content-Disposition: attachment”);
while(ob_get_level()){
ob_end_clean();
}
fpassthru($fp)
?>
各々のファイル名の渡し方の例として
出力ファイル名を【PATH_INFO】を使って「日本語名称」で渡し、
ダウンロードファイル名は【SHIFT-JIS】で【GET】で受け取る
使用例(new_page_10.php)
http://●●●●●●/●●●●●●/new_page_10.php/日本語名称.txt?file=aaaaaa.txt