Why is the downloaded file damaged? What's the problem? Please, what?

Source: Internet
Author: User
Why is the downloaded file damaged ?? What's the problem ??????? Please kindly advise & nbsp; function & nbsp; f ($ file_name, $ file_path) {$ file_name $ _ SERVER [DOCUMENT_ROOT]. $ file_path. $ file_name; & nbsp; $ file_pathiconv (UTF-8, why is the downloaded file damaged ?? What's the problem ??????? Please, what?
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 ??
------ Solution ----------------------
Tested, no problem.
------ Solution ----------------------
Thunder or something? The downloaded files may be incomplete.
------ Solution ----------------------
Take a look at the screenshot.

In addition, what do you use to open it. What is the code runtime environment?
------ Solution ----------------------
Tested, everything works.
------ Solution ----------------------
Are there any qq.jpg files in the E/Apache/htdocs directory?
------ Solution ----------------------
Open the image file in notepad to see what is there.
------ Solution ----------------------
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, "rb") in binary mode ");

In addition, your UTF-8 encoded program files must be saved in a format without a 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.