2011-03-24 89 views
0

我只是测试下面的这个下载脚本。下载工作正常,但下载的zip或rar档案总是损坏,无法打开。我在本地开发服务器以及我的主机帐户上进行了测试。保护下载

我只是想学习这是如何工作,但我并不真正了解它。

所有帮助表示赞赏!

测试代码:

<?php 
$is_logged_in = 1; 
$path_to_file = 'downloads/tes.zip'; 
$file_name = 'test.zip'; 

if ($is_logged_in == 1) 
{ 
    header("X-Sendfile: $path_to_file"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=\"$file_name\""); 
    exit; 
} 
?> 

<h1>Permission denied</h1> 
<p>Please Login first!</p> 
+0

在文本编辑器中打开文件。你看到了什么? – 2011-03-24 00:40:42

+0

你不发送任何数据,是吗? – Gumbo 2011-03-24 00:41:13

+0

您是否按照该代码的建议安装了'mod_xsendfile'? – icktoofay 2011-03-24 00:42:12

回答

1

它主要可能的是,你有什么追加/前置到文件中。尝试使用buffering并进行清洁。

<?php 
ob_start(); 
$is_logged_in = 1; 
$path_to_file = 'downloads/tes.zip'; 
$file_name = 'test.zip'; 

if ($is_logged_in == 1) 
{ 
    $fp = fopen($path_to_file, 'rb'); 

    if(is_resource($fp)) 
    { 
      ob_clean(); 
      header("Content-Type: application/force-download"); 
      header("Content-Length: " . filesize($path_to_file)); 
      header("Cache-Control: max_age=0"); 
      header("Content-Disposition: attachment; filename=\"$file_name\""); 
      header("Pragma: public"); 
      fpassthru($fp); 
      die; 
    } 
} else { 
    echo "<h1>Permission denied</h1>"; 
    echo "<p>Please Login first!</p>"; 
} 
+0

是否有使用mod_xsendfile的替代方法?有没有可能做到这一点,而无需安装模块? – usnidorg 2011-03-24 00:47:30

+0

谢谢,我在写它的时候保留头文件,它解决了IE下载的一些问题。 – Igor 2011-03-24 01:08:46

0

您是否配置了您的网络服务器(尤其是Apache)以加载mod_xsendfile?没有安装这个模块,你的脚本基本上什么都不做。