2013-02-21 54 views
-1

我目前使用此代码来隐藏下载链接,但它为某些用户提供了未完成的下载。我不知道为什么,但有太多用户向我报告这个问题。 我当前的代码是:隐藏下载链接的替代方法

file = "../".$realFileName; 
$fakeFileName= 'Upbaz.ir-'.base64_decode($_GET['ffname']); 
$fp = fopen($file, 'rb'); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=$fakeFileName"); 
header("Content-Length: " . filesize($file)); 
fpassthru($fp); 

任何人都知道其他的方法来隐藏下载链接?

回答

1

该脚本将处理文件的下载,它包括一个缓冲/不同类型的分机(如果你愿意的话)。隐藏链接的好方法是将文件放在受保护的目录中,并将链接存储在数据库中。用户看到与文件绑定的ID或会话,服务器找到并提供文件。

if ($fd = fopen ($fullPath, "r")) { 
    $fsize = filesize($fullPath); 
    $path_parts = pathinfo($fullPath); 
    $ext = strtolower($path_parts["extension"]); 
    switch ($ext) { 
     case "txt": 
      header("Content-type: application/txt"); // add here more headers for diff. extensions 
      header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download 
      break; 
     default: 
      header("Content-type: application/octet-stream"); 
      header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); 
    } 
    header("Content-length: $fsize"); 
    header("Cache-control: private"); //use this to open files directly 
    while(!feof($fd)) { 
     $buffer = fread($fd, 2048); 
     echo $buffer; 
    } 
} 
fclose ($fd); 

的.htaccess保护目录(你应该有一个目录只有这些文件

deny from all 

上面的脚本修改为你的:

$file = "../".$realFileName; 
$fakeFileName= 'Upbaz.ir-'.base64_decode($_GET['ffname']); 
if ($fd = fopen ($file, "r")) { 
     $fsize = filesize($file); 

     header("Content-type: application/octet-stream"); 
     header("Content-Disposition: attachment; filename=\"$fakename\""); 

     header("Content-length: $fsize"); 
     header("Cache-control: private"); //use this to open files directly 
     while(!feof($fd)) { 
      $buffer = fread($fd, 2048); 
      echo $buffer; 
     } 
    } 
    fclose ($fd); 
+0

我必须在服务器上安装patchinfo,因为我现在没有它 – 2013-02-21 17:00:52

+0

[pathinfo](http://php.net/manual/en/function.pathinfo.php)是一个内置的PHP函数,你真的需要Ť他的文件有不同的名称,或仅仅是为了保护他们找到真正的文件? – UnholyRanger 2013-02-21 17:02:21

+0

和它的类似我的代码没有不同...你确定我没有打破与此下载? – 2013-02-21 17:03:39

1

增加脚本运行时间EQ

@set_time_limit(120); 
+0

它在我的服务器的输入时间或EXCUTE时间? – 2013-02-21 16:55:22