繁体   English   中英

PHP:如何隐藏URL

[英]PHP: How to hide URL

我有以下download.php脚本下载文件,效果很好:

<?php

header("Content-Type: application/octet-stream");

$file = $_GET["file"];
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); 
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); 
} 
fclose($fp);

?>

我想要实现的是隐藏此文件所在的URL,以便当用户单击<a target="_blank" href="http://domain.com/files/download.php?file=filename.pdf">Download file</a> ,打开一个没有URL的新选项卡,并开始下载文件。 实际发生的是新选项卡打开并开始文件下载,但URL栏显示http://domain.com/files/download.php?file=filename.pdf

如果用php无法做到这一点,我该怎么做呢? 我看过几次没有显示URL的下载,所以我知道这有点可能。

编辑:这就是我想要这样做的原因:我们将发送一个带有文件下载链接的html邮件,托管此文件下载的网站不是发送邮件的公司的网站。

一如既往,非常感谢你。

这样做的一种典型方法是将您要提供的文件放在docroot之外。 您的下载脚本应该知道这个地方,并且必须处理所请求的文件名。

例如:

path/in/your/system/docroot/download.phppath/in/your/system/files/filename.pdf

如果有人请求download.php?file=filename.pdf您的脚本必须在path/in/your/system/files/查找此文件,并且必须处理它。

最后,这是不可能的,因为用户可以打开Goog​​le Inspector并查看“网络”标签。

编辑:如果是这种情况,您将使用JavaScript:

编辑2:如果弹出窗口被浏览器或插件阻止,请使用后备:

编辑3:如果JS被禁用:

<head><meta http-equiv="refresh" content="5;http://domain.com/files/download.php?file=filename.pdf"> 
    <script>
     var win = window.open("http://domain.com/files/download.php?file=filename.pdf");
      if (!win) {
               window.location = "http://domain.com/files/download.php?file=filename.pdf";      
      }
    </script>
  </head>

编辑4:如果您不希望用户完全看到用户站点的域,请让您的PHP脚本下载该文件,然后将其输出给用户:

header("Content-Type: application/octet-stream");

$file = $_GET["file"];
$file = 'http://otherdomain.com/location/' . $file; // This line should suffice for all that you're trying to do
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); 
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); 
} 
fclose($fp);

您可以使用iframe内容为该网址创建一个新窗口。

var win = window.open("", "Title", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));
win.document.body.innerHTML = "<iframe src='YOUR URL'></iframe>";

如果删除target="_blank"则不会打开新选项卡,只会下载文件。

将表单作为POST提交并使用$ _REQUEST获取文件:

$file = $_REQUEST["file"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM