使用 .htaccess 强制下载文件或图像 [英] Force a file or image to download using .htaccess

查看:37
本文介绍了使用 .htaccess 强制下载文件或图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 PHP 标头强制下载图片,但出现了多个问题.

I have been trying to force images to download using PHP Headers but there have been multiple problems.

那么,相反,我如何使用 .htaccess 以便当我链接到如下图像时:

So, instead, how can I use .htaccess so that when I link to an image like:

<a href="wallpapers/image.jpg">Download</a>

...不是在浏览器中打开该文件,而是实际下载该文件并将其保存到用户的计算机上.

...instead of opening that file in browser, the file is actually downloaded and saved to the user's computer.

如果有任何影响,我已经在我的项目中使用 .htaccess 来重写 URL.

I am already using .htaccess in my project to rewrite URLs if that affects anything.

推荐答案

将其放入您的 .htaccess

Put this into your .htaccess

<FilesMatch ".(?i:jpg|gif|png)$">
  Header set Content-Disposition attachment
</FilesMatch>

确保您已安装并启用 mod_headers.

Make sure you have mod_headers installed and enabled.

这篇关于使用 .htaccess 强制下载文件或图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆