-2

我有一个 S3 帐户来存储所有图像。

mysite.s3.amazonaws.com/imagename.JPG

这个 URL 是秘密的,我在哪里上传受限用户。我必须为这个秘密图像创建下载 URL。此下载网址仅供上传用户使用。

我如何在 php 中编写代码以安全地下载图像文件。

可能是 url 看起来像:mysite.com/restrictedimage.php?mid=xyztokens234

4

2 回答 2

1

您可以设计一个简单的数据库表(或配置文件)来记录令牌和受保护 URL 之间的相关性,然后在您的 restrictedimage.php 脚本中,您只需检查令牌并将用户重定向到实际链接。

相反,如果您希望对您的 URL 保密,您应该定期下载图像并将其缓存在您的服务器中,然后直接输出回图像,而不是重定向。

Example code for option 1:

$token = isSet($_GET['mid'])?$_GET['mid']:null;
if(!$token) die("No token");
$realURL = retrieveUrlFromToken($token);
if(!$realURL) die("Invalid token");
header("Location: http://mysite.s3.amazonaws.com/" . $realURL); 

Example code for option 2:

$token = isSet($_GET['mid'])?$_GET['mid']:null;
if(!$token) die("No token");
$realURL = retrieveUrlFromToken($token);
if(!$realURL) die("Invalid token");
$file = file_get_contents("http://mysite.s3.amazonaws.com/".$realURL);
header("Content-Type: application/force-download");
header("Content-Length: " . strlen($file));
header("Content-Transfer-Encoding: binary");
echo $file;

Notice that retrieveUrlFromToken could simply be an identity function, if you don't want to hide the real path, but I'd suggest you to do something better, otherwise the whole script is useless.

于 2013-03-30T11:48:37.833 回答
0

使用此代码下载图像文件

<a href="download.php?filename='imagename.JPG'>Download</a>

下载.php

<?php

    $filename ="http://mysite.s3.amazonaws.com/" . $_GET["filename"];
    $buffer = file_get_contents($filename);
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . strlen($buffer));
    header("Content-Disposition: attachment; filename=$filename");
    echo $buffer; 
?>
于 2013-03-30T11:44:04.110 回答