There may be a problem with the downloader code, causing the PDF file to become corrupted

Source: Internet
Author: User
Tags fread
The PDF file in the server is healthy: Verified by direct FTP download.
Some of the code for the downloader is as follows:

$file _size = filesize ($filedir);
Header ("content-type:application/octet-stream;charset=iso-8859-1");
Header ("Cache-control:private");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". $this->fjsize);
Header ("content-disposition:attachment; Filename= ". $this->delfilename);
$fp = fopen ($filedir, "R");
$buffer _size = 1024;
$cur _pos = 0;
while (!feof ($fp) && $filedir-$cur _pos > $buffer _size)
{
$buffer = Fread ($fp, $buffer _size);
Echo $buffer;
$cur _pos + = $buffer _size;
}

if ($file _size > 0)
$buffer = Fread ($fp, $file _size-$cur _pos);
Else
$buffer = Fread ($fp, $this->fjsize);
Echo $buffer;
Fclose ($FP);
Flush ();


Reply to discussion (solution)

$fp = fopen ($filedir, "R B"); Binary mode open, although Linux is not required, but still recommended to do so
Text Open file, 0x1a is considered the end of the file

while (!feof ($fp) && $filedir-$cur _pos > $buffer _size)
Writing
while (!feof ($FP))
Feof ($FP) is the end of the file.
Again $filedir-$cur _pos > $buffer _size no need
Double judgments can lead to errors, such as $filedir not $buffer _size integer times

In addition, you cannot export anything other than the contents of the file, including the BOM header

According to the moderator said changed. The error still occurs.
PS: The program has been working well in the past year and has only gone wrong in recent days.

uploaded a file with the following content

1234567890
Abcdefghijklmnopqrstuvwxyz

Then download to local and the results are as follows

? 2426153 Vanadium 4 stunt jljnimk first-step turbulence/(, *.) -+?? ?? ?? &

  • 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.