PHP: RFC-2231 How to encode UTF-8 String as Content-Disposition filename

后端 未结 1 1092
旧巷少年郎
旧巷少年郎 2020-12-21 01:34

Scenario: (in PHP) I have a form submission with a UTF-8 encoded string ($name) to support international characters. Upon submitting the form (

相关标签:
1条回答
  • 2020-12-21 02:05

    It is enough to encode the file name according to RFC 3986 by using rawurlencode()

    So all you need to do is change the header() line to:

    header("Content-Disposition: attachment; filename*=UTF-8''".rawurlencode($name));
    

    To answer the questions directly:

    1. The format is right but the text inside $name needs to be encoded with rawurlencode().
    2. rawurlencode() does the trick.
    0 讨论(0)
提交回复
热议问题