Last active
August 27, 2018 03:17
-
-
Save sharapeco/8571256 to your computer and use it in GitHub Desktop.
Content-Disposition: attachment で日本語ファイル名をダウンロードさせるテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$t = isset($_GET['t']) ? $_GET['t'] : null; | |
$org = '此のファイル test.php'; | |
$fn = array( | |
'A: UTF-8 Raw' => 'Content-Disposition: attachment; filename="' . $org . '"', | |
'B: UTF-8 URL Encoded' => 'Content-Disposition: attachment; filename="' . rawurlencode($org) . '"', | |
'C: UTF-8 Base64 Encoded' => 'Content-Disposition: attachment; filename="=?utf-8?B?' . base64_encode($org). '?="', | |
'D: RFC 2231' => 'Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode($org), | |
'E: Shift_JIS Raw' => 'Content-Disposition: attachment; filename="' . mb_convert_encoding($org, 'Shift_JIS', 'UTF-8') . '"', | |
'F: Shift_JIS URL Encoded' => 'Content-Disposition: attachment; filename="' . rawurlencode(mb_convert_encoding($org, 'Shift_JIS', 'UTF-8')) . '"', | |
); | |
if (isset($fn[$t])) { | |
header('Accept-Ranges: bytes'); | |
header('Content-Type: application/octet-stream'); | |
header($fn[$t]); | |
header('Content-Length: ' . filesize(__FILE__)); | |
readfile(__FILE__); | |
exit; | |
} | |
header('Content-Type: text/html; charset=UTF-8'); | |
?> | |
<ul> | |
<?php foreach ($fn as $i => $n) : ?> | |
<li><a href="?t=<?php echo rawurlencode($i); ?>">Download <?php echo htmlspecialchars($i); ?></a></li> | |
<?php endforeach; ?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment