Php 如何通过隐藏目录发送下载链接

Php 如何通过隐藏目录发送下载链接,php,laravel,laravel-5,Php,Laravel,Laravel 5,我想在用户购买产品时发送我的用户下载链接。我通过邮件发送下载链接。因此,当我发送下载链接时,我会像www.xyz.com/images/2343354.jpg 但我不想让他知道目录结构。意味着当用户尝试访问目录aftwards链接时,他可以下载我不想给该用户的其他图像。 有没有什么方法可以通过提供用户目录链接来避免下载 我的图像位于public/images目录中,因为我已经制作了一个laravel应用程序 向用户提供哈希url,如www.xyz.com/download?image=XNS23

我想在用户购买产品时发送我的用户下载链接。我通过邮件发送下载链接。因此,当我发送
下载链接时,我会像<代码>www.xyz.com/images/2343354.jpg
但我不想让他知道目录结构。意味着当用户尝试访问目录aftwards链接时,他可以下载我不想给该用户的其他图像。
有没有什么方法可以通过提供用户目录链接来避免下载


我的图像位于
public/images
目录中,因为我已经制作了一个laravel应用程序

向用户提供哈希url,如www.xyz.com/download?image=XNS2323NIEYEDUJES 您需要将具有相关哈希值的图像名称存储在表中。 在下载页面中,获取图像请求数据并从数据库中检索原始图像名称。 以及使用PHP下载代码进行下载

<?php
//get original filename with hash name from fb and store in a variable
if($dboriginalfilename!="")){
    $fileName = basename($_GET['file']);
    $path = set ur path;
    $filePath = $path.$fileName;
    if(!empty($fileName) && file_exists($filePath)){
        // Define headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$fileName");
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");
        // Read the file
        readfile($filePath);
        exit;
    } else {
        echo 'The file does not exist.';
    }
}

创建一个类似这样的JavaScript函数,它将下载文件,用户将不知道调用了什么url

下面是一些示例解决方案,您可以尝试

<a href="" id="yourlinkId" onclick="downloadfile('file');">download</a>
    <script>
    function downloadfile(filename){
    var file_path = 'host/path/'+filename+'.ext';
    var a = document.getElementById('yourlinkId'); //or grab it by tagname etc
    a.href = file_path;
    a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
    document.body.appendChild(a);
    a.href = '';//remove path after download
    }
    </script>

函数下载文件(文件名){
var file_path='host/path/'+filename+'.ext';
var a=document.getElementById('yourlinkId');//或通过标记名等获取它
a、 href=文件路径;
a、 download=file_path.substr(file_path.lastIndexOf('/')+1);
文件.正文.附件(a);
a、 href='';//下载后删除路径
}
在这段代码中,将显示一个普通链接,没有任何href和下载文件。然后单击该链接后,您将只传递文件名,文件将被下载,然后,如果用户将鼠标悬停在链接上,则不会显示任何路径

在这里,每次单击中的文件名('filename')都是动态的,并且脚本中的目录路径是静态的


请尝试一次。

用户如何访问您的“/images/2343355”目录以下载图像。授予安全权限定义单独的下载路径,该路径抽象目录结构,然后编写一些控制器逻辑以获取id并提供相应的图像我是否可以使用RewriteCond inside.htaccess解决此问题