php download link to rar

2.3k views Asked by At

I'm creating a website on my localhost that should let people download some .rar files.

In my index I've created some tags like this:

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

This is just an example of one single file, but in my php file 'download.php' I've got the problem when I want to download the .rar file

This is 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);
}
?>

It opens the stream in the right way, but I get that the file size is about 200 bytes and not the full length that is about 200MB.

How can I fix this problem?

1

There are 1 answers

3
user2182349 On

Remove the echo statement, there should not be any output before the headers. Change readfile($filename) to readfile($file)