Setting a filename inside the header with htaccess

大憨熊 提交于 2019-12-19 11:59:16

问题


we have an application which downloads the files from the server. After the download them it needs to save it, for that it uses the filename paramter which is comming from the responce header.

When i'm using the standart PHP download.php?id=downloadID i can set the headers with no problem.

The task now is that on the client server they do not have any php available, i need to set the filename inside of the responce header.

here is the htaccess what i have:

<FilesMatch "\.(?i:pdf)$">
    Header set Content-Type: application/octet-stream
    Header set Content-Disposition: "attachment; filename=FILE_NAME.pdf"
</FilesMatch>

And i have no idea how to parce the file name dynamicaly to the header using htaccess. I've google it looked for it but still no solution.

Any suggestions?


回答1:


I have no idea if it works, but this would be my first attempt.

RewriteRule [^/]+\.pdf$ - [E=FILENAME:$0]
<FilesMatch "\.(?i:pdf)$">
    Header set Content-Type application/octet-stream
    Header set Content-Disposition "attachment; filename=%{FILENAME}e" 
</FilesMatch>

EDIT Seems some apache installs prefix the env variable with REDIRECT_

RewriteRule [^/]+\.pdf$ - [E=FILENAME:$0]
Header set Content-Type application/octet-stream env=REDIRECT_FILENAME
Header set Content-Disposition "attachment; filename=%{REDIRECT_FILENAME}e" env=REDIRECT_FILENAME

Above code doesn't work out of the box. In my case, I needed another internal redirect.

But why would you need to set the filename in the header if the request URL contains the filename too? All browsers will save the file with under the name in the URL. So going to /path/test.pdf will result in browsers suggesting the filename test.pdf.



来源:https://stackoverflow.com/questions/9309102/setting-a-filename-inside-the-header-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!