Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Content-Disposition: attachment で日本語ファイル名をダウンロードさせるテスト
<?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
You can’t perform that action at this time.