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

前端 未结 18 1761
北荒
北荒 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 07:06

    I tested the following code in all major browsers, including older Explorers (via the compatibility mode), and it works well everywhere:

    $filename = $_GET['file']; //this string from $_GET is already decoded
    if (strstr($_SERVER['HTTP_USER_AGENT'],"MSIE"))
      $filename = rawurlencode($filename);
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    

提交回复
热议问题