php下载链接到rar

时间:2015-06-21 12:02:22

标签: php file download rar

我在本地主机上创建一个网站,让人们下载一些.rar文件。

在我的索引中,我创建了一些像这样的标签:

$filename = "Test001.rar";
<a href="download.php?file='.$filename.'">'.$filename.'</a>';

这只是一个单个文件的示例,但在我的php文件中&#39; download.php&#39;当我想下载.rar文件

时,我遇到了问题

这是download.php

<?php
echo "Welcome to Knowledge!";

if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file'])
{
    $file = $_GET["file"];
    $path = 'C:\xampp\htdocs\TestSite'."\\".$file;
}

$err = $path.'Sorry, the file you are requesting doesnt exist.';

if (file_exists($path) && is_readable($path))
{
    //get the file size and send the http headers
    $size = filesize($path);


    header('Content-Type: application/x-rar-compressed, application/octet-stream');
    header('Content-Length: '.$size);
    header('Content-Disposition: attachment; filename='.$file);
    header('Content-Transfer-Encoding: binary');

    readfile($filename);
}
?>

它以正确的方式打开流,但我知道文件大小约为200字节,而不是大约200MB的全长。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

删除 echo 语句,标题之前不应有任何输出。将 readfile($ filename)更改为 readfile($ file)