2012-10-08 60 views
0

我在一个从根目录向上一级的目录中有ZIP文件(以防止盗链等)。下面是代码:在0字节处的PHP下载

<?php 
    $filename = $_GET['id']; 
    if(!$filename){ 
     header("Location: index.html"); 
    } else { 
     function send_download($filename){ 
     $file_path = '../../../../downloads/' . $filename . '.zip'; 
     [email protected]($file_path); 
     header("Content-Type: application/x-zip-compressed"); 
     header("Content-disposition: attachment; filename=$filename"); 
     header("Content-Length: $file_size"); 
     readfile($file_path); 
     exit; 
    } 
    send_download($filename); 
    } 
    ?> 

所有的ZIP文件是罚款,但使用这种方法上的一个标签会导致下载到的文件大小为0字节! ?!:(

任何想法,为什么

非常感谢

+0

您找不到路径或文件名。 – bfavaretto

+3

为了测试起见,请在测试时使用文件大小($ file_path)中的文件的完整路径和GET RID OF HORRIBLE @! :) – Alfabravo

+0

你也应该不声明函数为'IF-THEN-else'声明 – Touki

回答

0

你可以尝试改变Content-type以下几点:

Content-type: application/x-zip; 

而在此之前,检查文件是否存在。

<?php 
    $filename = $_GET['id']; 
    if(!$filename){ 
     header("Location: index.html"); 
    } else{ 
    function send_download($filename){ 
    $file_path = '../../../../downloads/' . $filename . '.zip'; 
    if (file_exists($file_path)) 
    { 
    [email protected]($file_path); 
    header("Content-Type: application/x-zip-compressed"); 
    header("Content-disposition: attachment; filename=$filename"); 
    header("Content-Length: $file_size"); 
    readfile($file_path); 
    exit; 
    } 
    send_download($filename); 
    } 
    else 
    die("Error 404!"); 
    } 
?> 
+1

$ filename将不包含您正在执行的操作的必要路径。它需要是'file_exists($ file_path)'。 –

+0

@MarcB我已更新。 :) –

+0

感谢家伙们,仍然没有运气,我害怕:(任何进一步的想法? – HedKandiMan

0
  1. header(“Location:http://www.fqdn.tld/file.ext”);

  2. 基于_GET [“id”]为> 0且不为空,您创建了一个已被直接使用的函数,因此您不需要添加更多行代码。

  3. 我看到您已经添加,因为人们先前评价的“@”符号,但是,你开始修复这个问题的唯一方法是:

    • 删除@符号作为你永远不应该使用它,非常糟糕的做法,你应该满足你的代码中的所有异常,它使一个非常好的程序员。 (PHP脚本)

    • error_reporting(E_ALL);

    • 广场B点)在它自己的路线,而是< PHP之后 - 所以我们可以看到错误的完整列表。

  4. 你的代码是正确的,你有问题的权限,确保Apache在你的“文件”版本库访问,您可以检查该文件是存在的事实表明轻微的权限和文件存在,0字节被返回的地方表明阅读权限在Apache级别被拒绝。

+0

谢谢理查德。我如何更改权限?我使用共享主机,所以这可能是一个问题。 – HedKandiMan

+0

如果你使用的是FTP程序,通常通过右键单击命令来支持它,然后755就可以找到。但是,这可能是问题存在的地方。我建议把你的文件放在你的根目录下,但是一个misc子文件夹的名字,例如:把这些文件称为/g4445AF/fileid.zip –

+0

感谢Richard,对于所有的递归子目录,权限已经设置为755 ... – HedKandiMan

0

右键,有点谷歌搜索和一些鲜血,汗水和泪水之后,我终于找到了解决办法!

<?php 
    $filename = $_GET['id']; 
    header('Content-type: application/zip'); 
    header("Content-Disposition: attachment; filename=" . $filename); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    readfile('../../downloads/' . $filename . ".zip"); 
    exit; 
?> 

非常感谢所有的人对此有所了解!

+0

测试一下:http://artists.platform-a.org.uk/axm – HedKandiMan

0

这里是与文件大小的代码:

<?php 
    $filename = $_GET['id']; 
    $file = "../../downloads/" . $filename . ".zip"; 
    header('Content-type: application/zip'); 
    header("Content-Disposition: attachment; filename=" . $file); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Content-length: " . filesize($file)); 
    readfile($file); 
    //echo filesize($file); 
    exit; 
?> 
0

只有一个错误,我们都在下面做检查。

readfile($ file); // $文件应该是相对不绝对的。而已。

感谢,

Anirudh苏德。