如何强制下载没有脚本的图像,但是$ _GET? [英] How to force download an image without a script, but with $_GET?

查看:153
本文介绍了如何强制下载没有脚本的图像,但是$ _GET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这种方法现在在大约三个站点上使用,包括Facebook,Dropbox和微软的Skydrive。它像这样工作。假设您想要查看图像而不下载,那么您只需执行此操作。

I have seen this method being used on about three sites now, including Facebook, Dropbox and Microsoft's Skydrive. It works like this. Let's say you want to look at the image without downloading, then you'd just do this.

https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-xxxx/xxx_xxxxxxxxxxxxxxx_xxxxxxxxx_o.jpg

但是如果我想要下载,我会添加?dl = 1

But if I want to download it, I'd add ?dl=1

https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-xxxx/xxx_xxxxxxxxxxxxxxx_xxxxxxxxx_o.jpg?dl=1

简单好听吗那么服务器端可能不容易,这就是我的问题。如果.jpg文件是PHP脚本,$ _GET参数指向图像,另一个参数将指定图像是否要下载,我将知道如何执行此操作。但是情况并非如此。

Easy peasy right? Well, it's probably not easy on the server side, and this is where my problem is. I would know how to do this if that .jpg-file was a PHP script and the $_GET parameter pointed to the image and another parameter would specify whether the image were to be downloaded or not. But that's not the case.

那么我尝试了什么方法?没有。因为我真的不知道这是如何工作的,这对我来说就像魔术。也许这是你在.htaccess中做的?对我来说听起来很合理,但经过一段时间的谷歌搜索,我没有找到任何甚至接近我要求的东西。

So, what methods did I try? None. Because I honestly have no idea how this works, it's like magic to me. Maybe it's something that you do in .htaccess? That sounds reasonable to me, but after a while of googling I didn't find anything even close to what I'm asking for.

推荐答案

您有一些选项。

一个选项是使用PHP脚本而不是.jpg文件。因此,您的URL将指向一个PHP文件,并在PHP文件中,您将执行以下操作:

One option would be to use a PHP script instead of the .jpg file. So your URL would point to a PHP file and in the PHP file you would do something like this:

header('Content-Type: image/jpeg');

if ($_GET['dl'] == 1)
    header('Content-Disposition: attachment; filename="downloaded.jpg"');

$file = $_GET["file"];
// do some checking to make sure the user is allowed to get the file specified.
echo file_get_contents($file);

另一个选择是使用 mod_rewrite 来检查?dl = 1 ,如果找到,重定向到将下载文件的PHP脚本(与上述相同)。

Another option would be to use mod_rewrite in your .htaccess file to check for ?dl=1 and if found, redirect to the PHP script that will download the file (the same way as above).

我确定有更多的选择,但是那两个是现在唯一出现在我头上的人。

I'm sure there are more options, but those two are the only ones popping into my head right now.

这篇关于如何强制下载没有脚本的图像,但是$ _GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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