Skip to content

Instantly share code, notes, and snippets.

@virusvn
Forked from sharapeco/download_test.php
Created December 13, 2015 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virusvn/f4a9910ed1d4cdebe25a to your computer and use it in GitHub Desktop.
Save virusvn/f4a9910ed1d4cdebe25a to your computer and use it in GitHub Desktop.
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