2014-11-06 59 views
0

时如何加密的URL我有这样的代码:PHP jQuery的下载文件

$(li.AandCattachment.a download).click(function() { 
$('#down').val($(this()); 

location.href('path/to/file?=' + $('#down').val()); 

} 

,但我需要这样,这将是固定的URL加密,它不能被其他用户复制并看到所有通过复制文件的URL和逐个更改网址的文件..

你能帮助我吗?

回答

0

要实现不会只涉及客户端代码,但是服务器端代码太什么..

我可不是给你100%的解决方案,你将不得不做一些额外的工作(你会需要一种机制来验证我自己可以不知道你是怎么验证的用户)无法猜测的URL,但是这将指向你的方式:

做一个PHP文件来处理文件下载这样

$crypted_url = $_GET["url_to_file_encripted_somehow"]; 

$file_path = get_file_path_from_crypted_url($crypted_url); 

if ($file_path === false){ 
    include "403.html"; 
    die; 
} 

$filename = "whatever"; 

header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=$filename.".pathinfo($file_path, PATHINFO_EXTENSION)); 
echo readfile($documento->link_archivo()); 

如果你解释机制,我可能会有更多的帮助您想使用sm来验证用户。

1

您可以将这些文件放在webroot之外,这样它们就不能直接使用,然后通过php提供。在这个例子中,我使用的关联数组映射“随机”字符串的实际文件,使文件名不能简单地猜测:

//download.php 
$pathmap=[ 
    'hkd654gk'=>'../file1.jpg', 
    'k735hs87'=>'../file2.jpg', 
    'qwcv13v5'=>'../file3.jpg', 
    ]; 

$securepath = isset($_GET['path'])?$_GET['path']:''; 
if(!isset($pathmap[$securepath])) 
    die('file not found'); 
header('Content-Type: image/jpeg'); 
readfile($pathmap[$securepath]); 

要访问文件1,网址是:

example.com/download.php?path=hkd654gk