PHP output CSV file garbled

Source: Internet
Author: User

Problem: the PHP input user list is a csv file, which is displayed as garbled characters when opened in excel, and opened normally with notepad or ue. Check that the file encoding is UTF-8.

Solution:
Function down_file ($ filepath, $ filename)
{
If (! File_exists ($ filepath ))
{Www.2cto.com
Echo "backup error, download file no exist ";
Exit ();
}
Ob_end_clean ();
Header ('content-Type: application/download ');
Header ("Content-type: text/csv ");
Header ('content-Disposition: attachment; filename = "'. $ filename .'"');
Header ("Content-Encoding: binary ");
Header ("Content-Length:". filesize ($ filepath ));
Header ("Pragma: no-cache ");
Header ("Expires: 0 ");
Readfile ($ filepath );
$ E = ob_get_contents ();
Ob_end_clean ();
}

Using fnamepolic'usersdata.csv ';
$ Handle = fopen ($ fname, 'wb ');
$ StrUsersData = iconv ('utf-8', 'gb2312', $ strUsersData); // convert the Encoding
If (fwrite ($ handle, $ strUsersData) = false ){}
Fclose ($ handle );
Down_file(%fname,'555.csv ');

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.