The file size does not match the size of the server source file after downloading the PHP download file feature

Source: Internet
Author: User
Tags chop fread php download ranges
1. Functions based on downloading files on the Web

    Public Function Putfile ($file _dir, $file _name) {$file _dir = Chop ($file _dir);//Remove extra spaces in the path//Get the path of the file to download            if ($file _dir! = ") {$file _path = $file _dir;            if (substr ($file _dir,strlen ($file _dir) -1,strlen ($file _dir))! = '/') $file _path. = '/';        $file _path. = $file _name;        } else $file _path = $file _name;            Determine if the file to be downloaded exists if (!file_exists ($file _path)) {//alert (' Sorry, the file you want to download does not exist ');        return false;        } $file _size = FileSize ($file _path);        Header ("CONTENT-TYPE:APPLICATION/OCTET-STREAM;CHARSET=GBK");        Header ("Accept-ranges:bytes");        Header ("Accept-length: $file _size"); Header ("content-disposition:attachment;        Filename= ". $file _name);        $fp = fopen ($file _path, "R");        $buffer _size = 1024;        $cur _pos = 0; while (!feof ($fp) && $file _size-$cur _pos> $buffer _size) {$buffer = Fread ($fp, $bUffer_size);            Echo $buffer;        $cur _pos + = $buffer _size;        } $buffer = Fread ($fp, $file _size-$cur _pos);        Echo $buffer;        Fclose ($FP);    return true; }

Make sure it is the same file but the downloaded file appears smaller than the source file.

Reply content:

1. Functions based on downloading files on the Web

    Public Function Putfile ($file _dir, $file _name) {$file _dir = Chop ($file _dir);//Remove extra spaces in the path//Get the path of the file to download            if ($file _dir! = ") {$file _path = $file _dir;            if (substr ($file _dir,strlen ($file _dir) -1,strlen ($file _dir))! = '/') $file _path. = '/';        $file _path. = $file _name;        } else $file _path = $file _name;            Determine if the file to be downloaded exists if (!file_exists ($file _path)) {//alert (' Sorry, the file you want to download does not exist ');        return false;        } $file _size = FileSize ($file _path);        Header ("CONTENT-TYPE:APPLICATION/OCTET-STREAM;CHARSET=GBK");        Header ("Accept-ranges:bytes");        Header ("Accept-length: $file _size"); Header ("content-disposition:attachment;        Filename= ". $file _name);        $fp = fopen ($file _path, "R");        $buffer _size = 1024;        $cur _pos = 0; while (!feof ($fp) && $file _size-$cur _pos> $buffer _size) {$buffer = Fread ($fp, $bUffer_size);            Echo $buffer;        $cur _pos + = $buffer _size;        } $buffer = Fread ($fp, $file _size-$cur _pos);        Echo $buffer;        Fclose ($FP);    return true; }

Make sure it is the same file but the downloaded file appears smaller than the source file.

It's normal that different systems have differences.

Write too complex, the more complex the more error-prone, not sure if you have the length of management problems, the following code is sufficient.

$fp = fopen($file_path,"r");$content = '';while($_content = fread($fp, 1024)) $content.= $_content;fclose($fp);

If the file is only 100 bytes, fread ($FP, 1024) will not get 1024 bytes.

Other than that... I think that CHARSET=GBK should be removed without the need to specify the encoding.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.