How to force the user to download a file, not to view it?

前端 未结 3 1059
挽巷
挽巷 2020-12-04 03:32

I have a download page where i have kept musics, pictures, ebooks and etc. Whenever users click at the image it opens it, and same things happen with .pdf files if the user

相关标签:
3条回答
  • 2020-12-04 03:56

    With Apache mod_headers:

    SetEnvIf Request_URI "([^/]+\.jpg)$" REQUESTED_FILE_BASENAME=$1
    SetEnvIf Request_URI "([^/]+\.mp3)$" REQUESTED_FILE_BASENAME=$1
    SetEnvIf Request_URI "([^/]+\.pdf)$" REQUESTED_FILE_BASENAME=$1
    Header set Content-Disposition "attachment; filename=\"%{REQUESTED_FILE_BASENAME}e\"" env=REQUESTED_FILE_BASENAME
    

    The instructions can be placed inside a .htaccess file which itself must go in the directory containing your media. You use just about any extension. The following line might also help against aggressive plugins:

    Header set Content-Type "application/octet-stream" env=REQUESTED_FILE_BASENAME
    
    0 讨论(0)
  • 2020-12-04 04:14

    You have to add a header to force the download.

    header("Content-Disposition: Attachment")
    header("Content-Disposition: Attachment;filename=FILENAME_HERE")
    

    from http://www.symkat.com/force-download-with-http-headers

    0 讨论(0)
  • 2020-12-04 04:19
    header('Content-disposition: attachment; filename=NAME_OF_YOUR_FILE');
    
    0 讨论(0)
提交回复
热议问题