Solution to PHP file download corruption and opening garbled characters

Source: Internet
Author: User

Recently updated a project. The download part follows the download code of the previous project. But this time there was a small problem. The downloaded word and other files will prompt garbled characters, when downloading files such as rar, the system prompts that the file has been damaged and the cause has not been determined for a long time. After the problem is solved in an uncertain way, record the problem and wait for additional reasons.

$ Extend = explode ('.', $ file_info-> path );
$ Ext = array_pop ($ extend );
$ File_name = $ file_info-> file_name. '.'. $ ext;

$ File = fopen ($ file_info-> path, "r ");
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". filesize ($ file_info-> path ));
Header ("Content-Disposition: attachment; filename =". $ file_name );

Ob_clean (); # The newly added code
Flush (); # New code
Echo fread ($ file, filesize ($ file_info-> path ));
Fclose ($ file );

Note: If the downloaded file name is Chinese, it will be garbled and can be converted to utf8 or pinyin.

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.