How to encode the filename parameter of Content-Disposition header in HTTP?

前端 未结 18 1750
北荒
北荒 2020-11-21 06:15

Web applications that want to force a resource to be downloaded rather than directly rendered in a Web browser issue a Content-Disposition hea

18条回答
  •  走了就别回头了
    2020-11-21 06:52

    In PHP this did it for me (assuming the filename is UTF8 encoded):

    header('Content-Disposition: attachment;'
        . 'filename="' . addslashes(utf8_decode($filename)) . '";'
        . 'filename*=utf-8\'\'' . rawurlencode($filename));
    

    Tested against IE8-11, Firefox and Chrome.
    If the browser can interpret filename*=utf-8 it will use the UTF8 version of the filename, else it will use the decoded filename. If your filename contains characters that can't be represented in ISO-8859-1 you might want to consider using iconv instead.

提交回复
热议问题