当用户想要从我的网站下载文件时,用户必须单击如下链接
https://www.example.com/download.php?aaa=111&bbb=222
下载.php
<?PHP
session_start();
include("connect.php");
$aaa = mysql_real_escape_string($_GET[aaa]);
$bbb = mysql_real_escape_string($_GET[bbb]);
if(($aaa = '111')&($bbb = '222')) // this line is example for ask stackoverflow //
{
$filePath_try_to_download = 'attachments_files/test.pdf';
if(file_exists($filePath_try_to_download))
{
$fileSize = filesize($filePath_try_to_download);
$fileName = "test.pdf";
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath_try_to_download);
exit();
}
}
?>
我想知道用户何时从此链接下载文件 https://www.example.com/download.php?aaa=111&bbb=222
用户是否可以获取我在服务器上的文件路径(attachments_files/test.pdf
)。如果用户可以获得我的文件路径,我如何隐藏它? (该目录中的文件非常重要
)
最佳答案
由于我是通过手机发表评论的,所以他们无法真正解释太多,所以这是您的答案。
I want to know when user download file from this link https://www.example.com/download.php?aaa=111&bbb=222 user can get my file path on server or not (attachments_files/test.pdf).
否,用户无法看到您通过readfile()
正在读取的文件路径。他们根本无法找到该文件的位置。
如果您想消除人们猜测文件路径的任何机会,只需将这些文件放在您的 Web 根文件夹之外,然后从那里 readfile()
它们即可。
$filePath_try_to_download = 'attachments_files/test.pdf';
该路径只有您的 PHP 代码知道,用户看不到该路径,因此他们不知道您从哪里读取他们正在下载的文件,只需消除猜测的机会:)
显然您必须确保访问此网址https://www.example.com/download.php?aaa=111&bbb=222
否则有什么意义!
关于php - 如何使用php向用户隐藏下载文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35473994/