Why is the downloaded file corrupted? Please, what?

Source: Internet
Author: User
Why is the downloaded file corrupted? Function f ($ file_name, $ file_path ){
$ File_name = $ _ SERVER ['document _ root']. $ file_path. $ file_name;
$ File_path = iconv ("UTF-8", "gb2312", $ file_name );
If (! File_exists ($ file_path ))
// If (! File_exists ($ file_name ))
{
Echo "does not exist ";
Return;
}
$ Fp = fopen ($ file_path, "r ");
$ File_size = filesize ($ file_path );

Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length: $ file_size ");
Header ("Content-Disposition: attachment; filename =". $ file_name );
$ Buffer = 1024;
While (! Feof ($ fp ))
{
$ File_data = fread ($ fp, $ buffer );
Echo $ file_data;
}
Fclose ($ fp );
}
F ("qq.jpg ","/");
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??


Reply to discussion (solution)

Tested, no problem.

Why can't I?

Thunder or something? The downloaded files may be incomplete.

It's okay to open the picture. it's not possible to download the tested code.

Take a look at the screenshot.

In addition, what do you use to open it. What is the code runtime environment?

Tested, everything works.

Image Viewer for win7

Google Chrome apache php5.3

Are there any qq.jpg files in the E/Apache/htdocs directory?

Yes, you cannot see it ....

Open the image file in notepad to see what is there.

A bunch of garbled characters

Your code includes $ file_path = iconv ("UTF-8", "gb2312", $ file_name );
Indicates that your php works in the window system.

The window system strictly distinguishes between text files and binary files.
However, you can use $ fp = fopen ($ file_path, "r") to open binary files in text mode.

So, because you violate the rules, you cannot get the correct result.

You need to open the binary file $ fp = fopen ($ file_path, "r B") in binary mode ");

In addition, your UTF-8 encoded program files must be saved in a format without a BOM header.

Thanks. you can save it in the format without BOM header.

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.