[英]PHP Jquery how to encrypt the url when downloading a file
我有以下代碼:
$(li.AandCattachment.a download).click(function() {
$('#down').val($(this());
location.href('path/to/file?=' + $('#down').val());
}
但是我需要對URL進行加密,以便它將得到保護,並且不能被其他用戶復制,並且通過復制文件的URL並逐個更改URL來查看所有文件。
你能幫我嗎?
您可以將文件放在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]);
要訪問file1,URL為:
example.com/download.php?path=hkd654gk
您要實現的目標不僅涉及客戶端代碼,還涉及服務器端代碼。
我沒有為您提供100%的解決方案,您將不得不做一些額外的工作(您將需要一種機制來驗證我自己無法猜測的網址,而不知道您如何驗證用戶),但這將為您指明道路:
制作一個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());
如果您解釋要用於驗證用戶的機制,我可能會提供進一步的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.