简体   繁体   中英

How to make php file 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? 对照

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM