PHP output CSV file garbled problem_PHP tutorial

WBOY
Release: 2016-07-13 17:54:20
Original
1231 people have browsed it

Problem: The PHP input user list is a csv file. When opened with excel, it is displayed as garbled characters. When opened with notepad or ue, it is normal. When viewing the file encoding, it is displayed as 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();
}

$fname='usersdata.csv';
$handle=fopen($fname,'wb');
$strUsersData =iconv('utf-8','gb2312',$strUsersData);//Convert encoding
if(fwrite($handle,$strUsersData)==false){}
fclose($handle);
down_file($fname,'555.csv');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477958.htmlTechArticleProblem: PHP input user list is a csv file. When opened with excel, the display is garbled, but it is normal when opened with notepad or ue. .View the file encoding and display it as utf-8. Solution: function down_file($filepath,$filename...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template