Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make php file download?

Tags:

php

download

So I have this in my PHP script on my test site:

$file="clip.mp4";
$fake="clip_testing.mp4";
$fsize = filesize($file);
header("Content-Length: $fsize");
header("Content-Disposition: attachment; filename=$fake");
header("Content Type: application/download");

set_time_limit(0);
$fs = @fopen($file,"rb");

while(!feof($fs))

{
    print(@fread($fs, 1024*8));
    ob_flush();
    flush();
}

When I use this to in a link and download the file, the first 7 bytes of my files are changed .. therefore I have to edit the program before I can use it. I have added a picture to make it more clear as to what I am talking about when I say the first 7 bytes have changed. Am I missing headers? Can anyone help me? Comparison

EDIT: If you guys are having a problem viewing the picture because of its size, look at the image in a new tab without the formatting so it will be its original size.

like image 806
Andrew Butler Avatar asked Feb 22 '23 07:02

Andrew Butler


1 Answers

I think you have 4 empty lines before your <?php tag.

That 8 Bytes are 4 times linebreaks \r\n.

like image 80
rekire Avatar answered Feb 23 '23 20:02

rekire