search
HomeBackend DevelopmentPHP Tutorialphp导入csv文件碰到乱码问题的解决方法_PHP

今天主要是想写一个php导入csv文件的方法,其实网上一搜一大把。都是可以实现怎么去导入的。但是我导入的时候遇到了两个问题,一个是在windows上写代码的时候测试发生了乱码问题,然后解决了。第二个是提交到linux系统上的时候又发生了乱码。我开始还不清楚是乱码的原因,一开始我还以为是代码svn提交发生的错误,到最后我在我的一个群里提问了一下,一朋友是做phpcms的,他说他遇到从Windows提交到Linux的时候刚开始也总是发生错误,后来排查原因就是乱码导致成的。下面切入正题看怎么解决两个问题的吧!

问题一解决:

php读取csv文件,在windows上出现中文读取不到的情况,本人立马想到一个函数mb_convert_encoding();作如下设置 $str = mb_convert_encoding($str, "UTF-8", "GBK");然后就可以了。当然你也可以用iconv();作如下设置iconv(‘GBK',”UTF-8//TRANSLIT//IGNORE”,$str);这两个函数来解决在windows上面发生乱码的问题。

问题二解决:

php读取csv文件,在linux上出现中文读取不到的情况,百度,google后找到解决办法

就是添加了一行代码setlocale(LC_ALL, 'zh_CN');对,亮瞎你的眼了吧。就这么简单,如果你不知道,可能会花很多时间去解决这个问题。
PHP setlocale() 函数解释
定义和用法

setlocale() 函数设置地区信息(地域信息)。

地区信息是针对一个地理区域的语言、货币、时间以及其他信息。该函数返回当前的地区设置,若失败则返回 false。
以下是在资料上收集常用的地区标识:

复制代码 代码如下:
zh_CN GB2312
en_US.UTF-8 UTF-8
zh_TW BIG5
zh_HK BIG5-HKSCS
zh_TW.EUC-TW EUC-TW
zh_TW.UTF-8 UTF-8
zh_HK.UTF-8 UTF-8
zh_CN.GBK GBK

例如、
utf-8: setlocale(LC_ALL, ‘en_US.UTF-8′);
简体:setlocale(LC_ALL, ‘zh_CN');

之所以给大家讲 setlocale()这个函数,是因为我导入csv文件到linux系统的时候发生了乱码,包括用了mb_convert_encoding()和iconv()两个函数都是没搞定最后问题的。最后就加了这一句setlocale(LC_ALL, ‘zh_CN');加在导入csv文件开始的代码前面就轻松搞定了,然后我又找了一下资料,发现fgetcsv()函数对区域设置是敏感的。比如说 LANG 设为 en_US.UTF-8 的话,单字节编码的文件就会出现读取错误,所以我们需要对其进行区域性的设置。特分享给大家。

我还尝试用了以下代码也没能搞定,这些都是生成csv文件的header的设置。可能在我这里不起作用,但是在你那里也说不定哦。所以我都整理出来,尽可能的帮助遇到导入csv文件乱码的同行,因为在没办法的情况下真的太难处理了。大家可以都试试!总有一个是属于你的。

复制代码 代码如下:
$csvContent="csvzero,csvone,csvtwo,csvthree,csvfour,csvfive";
header("Content-Type: application/vnd.ms-excel; charset=GB2312");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=CSV数据.csv ");
header("Content-Transfer-Encoding: binary ");
$csvContent = iconv("utf-8","gb2312",$csvContent);
echo $csvContent;
exit;
?>

下面就再来具体看看php导入csv文件的代码:

两个函数简单介绍一下,

mb_detect_encoding()检测到的字符编码,或者无法检测指定字符串的编码时返回FALSE。

fgetcsv() 函数从文件指针中读入一行并解析 CSV 字段。与fgets() 类似,不同的是 fgetcsv() 解析读入的行并找出 CSV 格式的字段,然后返回一个包含这些字段的数组。fgetcsv() 出错时返回 FALSE,包括碰到文件结束时。

注释:从 PHP 4.3.5 起,fgetcsv() 的操作是二进制安全的。

注释:CSV 文件中的空行将被返回为一个包含有单个 null 字段的数组,不会被当成错误。

注释:该函数对区域设置是敏感的。比如说 LANG 设为 en_US.UTF-8 的话,单字节编码的文件就会出现读取错误。

注释:如果碰到 PHP 在读取文件时不能识别 Macintosh 文件的行结束符,可以激活 auto_detect_line_endings 运行时配置选项。
复制代码 代码如下:
setlocale(LC_ALL, 'zh_CN'); //设置地区信息(地域信息)
$file = $_FILES['files'];
$file_type = substr(strstr($file['name'],'.'),1);
if ($file_type != 'csv'){
echo "";
exit;
}
$handle = fopen($file['tmp_name'],"r");
$file_encoding = mb_detect_encoding($handle);
if ($file_encoding != 'ASCII'){
echo "";
exit;
}
$row = 0;
$str="";
$sy="";
while ($data = fgetcsv($handle,1000,',')){
$row++;
if ($row == 0)
continue;
$num = count($data);
for ($i=0; $i$str = (string)$data[$i].'|';
$str = mb_convert_encoding($str, "UTF-8", "GBK"); //已知源码为GBK,转换为utf-8
$sy .= $str; //我这里做的比较复杂,是用'|'将csv文件里面的内容用'|'全部拼起来,因为我导入的是商品信息,需要根据用户需
//要导入的数据去定义哪些数据是需要导入的。
}
}
if ($sy) { $sy = rtrim($sy, '|'); }
$arr = explode('|',$sy);
$key = array_slice($arr,0,$num); //这个数组就是csv文件里面标题,就是商品id,标题,卖点等等的数据
$skey = array();
$length = array();
$co = count($arr);
$p = $co/$num; //求出要取出的数据的长度
for($j=0;$j$offset=($j-1)*$num; //偏移量,就像分页一样,我这里根据偏移量取出的一个数组就是一个商品的信息。
if($j==0){
$length[] = array_slice($arr,0,$num);
}else{
$length[] = array_slice($arr,$num+$offset,$num);//取出有哪些字段和商品
}
}
$arrtitle = array();
$arrfileds = array();
$arrtagname = DB::select('字段标识', '字段名称')->from('字段表')->fetch_all();
foreach ($arrtagname as $value) {
$arrfileds[$value['fileds_tags']] = $value['fileds_name'];
}
foreach ($fileds as $v)
{
$temarr= explode('-', $v);
if (isset($temarr[0]) && !empty($temarr[0])) {
if (isset($temarr[1]) && !empty($temarr[1])) {
if ($temarr[1] == 'wenben') {
$arrtitle[] = $arrfileds[$temarr[0]].'文本';
}
} else {
if ($temarr[0] != 'pic') { //是取出字段是图片就给去掉
$arrtitle[] = $arrfileds[$temarr[0]];
}
}
}

}

$skey = array();
$order = array();
$order[] = 'act_tag';
$order[] = 'channel_tag';
$order[] = 'created_time';
$order[] = 'orderby';
$rows ='';
$f = $co/$num;//求出有多少件商品
for($p=0;$p//这里就是根据自己的需求查出自己需要的数据,通过用户需要的商品字段标识查出表里相对应的英文标识。
$skey[]= DB::select('字段标识')->from('字段表')->where('字段名称', '=', $arrtitle[$p])->fetch_row();
$rows .= $skey[$p]['字段标识'].'|';
}
if($rows){ $rows = rtrim($rows,'|'); }
if(!empty($rows)){ $exrows = explode('|',$rows); }else{ $exrows = array(); }
$skeys = array_merge($order,$exrows);
$count1 = count($skeys); //字段的个数
if(!empty($length)){
for($x=1;$x$orders = array();
$orders[] = $act_tag;
$orders[] = $channel_tag;
$orders[] = time();
$newlen = array_merge($orders,$length[$x]);
if($count1 !== count($newlen)){ //如果商品字段的长度和商品的长度不等就证明用户有哪个字段没录入
$newrs = array();
echo "";
fclose($handle);
exit();
}else{ //start
$arrimport = array_combine($skeys,$newlen); //如果两个数组是相等的我就合并数组,并把导入csv里面的日期改为时间戳存储到数据库
if(!empty($arrimport['start_time'])){ $sta = strtotime($arrimport['start_time']); }else{ $sta=(int)0; }
if(!empty($arrimport['end_time'])){ $end = strtotime($arrimport['end_time']); }else{ $end=(int)0; }
$arrtime=array('start_time'=>$sta,'end_time'=>$end);
if(!empty($arrimport['start_time']) && !empty($arrimport['end_time'])){
$newrs=array_merge($arrimport,$arrtime);
}else{
$newrs = array();
echo "";
fclose($handle);
exit();
}
if(count($skeys) == count($newrs)){
DB::insert('商品表', array_values($skeys))
->values(array_values($newrs))
->execute();
}
} //end
}
}
if($row-1==(int)0){
echo "";
}else{
echo "

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software