search
Homephp教程php手册php导入csv文件以及碰到乱码解决方法
php导入csv文件以及碰到乱码解决方法Jun 06, 2016 pm 07:45 PM
csvphpGarbled charactersskyimportdocumentmethodsolve

今天主要是想写一个php导入csv文件的方法,其实网上一搜一大把。都是可以实现怎么去导入的。但是我导入的时候遇到了两个问题,一个是在windows上写代码的时候测试发生了乱码问题,然后解决了。第二个是提交到linux系统上的时候又发生了乱码。我开始还不清楚

    今天主要是想写一个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文件乱码的同行,因为在没办法的情况下真的太难处理了。大家可以都试试!总有一个是属于你的。。。

<?php $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 运行时配置选项。

   <?php setlocale(LC_ALL, 'zh_CN');  //设置地区信息(地域信息)
    $file = $_FILES['files'];
    $file_type = substr(strstr($file['name'],'.'),1);
   if ($file_type != 'csv'){
   echo "<script type=\"text/javascript\">alert(\"文件格式错误,请重新上传!\"); "; 
   exit;
   }
   $handle = fopen($file['tmp_name'],"r");
   $file_encoding = mb_detect_encoding($handle);
   if ($file_encoding != 'ASCII'){
   echo "<script type='\"text/javascript\"'>alert(\"文件编码错误,请重新上传!\"); </script>"; 
   exit;
   }
  $row = 0;
  $str="";
  $sy="";
  while ($data = fgetcsv($handle,1000,',')){
    $row++;
    if ($row == 0)
    continue;
    $num = count($data);
    for ($i=0; $ifrom('字段表')->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<count 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;$xalert(\"<font color="#f00;">".'请检查第,'.($x-1).'件商品!'.'导入失败!'."</font>"); "; 
    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 "<script type='\"text/javascript\"'>alert(\"<font color=#f00;>".'请检查第,'.($x-1).'件商品!'.'导入失败!'.""); </script>"; 
  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 "<script type='\"text/javascript\"'>alert(\"<font color=#f00;>".'您导入的商品为空!'.""); </script>";  
            }else{ 
echo "<script type='\"text/javascript\"'>alert(\"<font color=#f00;>".'成功导入'."<font color=#f00;>".($row-1)."".'件商品!'."");
      }
   fclose($handle);
   }
?></script></count>

以上是我工作需要所做的csv导入处理,可能和你的导入方式不同,但是部分代码总会对你有帮助!

以下是简单导入:


导入模板
$row"; //可以知道总共有多少行 $row++; if ($row == 1) continue; $num = count($data); // 这里会依次输出每行当中每个单元格的数据 for ($i=0; $i"; // 在这里对数据进行处理 } } fclose($handle); } ?>

方法二:

<?php $fp = fopen('csv文件名', 'w');
$rs = mysql_query('select * from tbl_name');
while($row = mysql_fetch_assoc($rs) {
  fputcsv($fp, $row);
}
fclose($fp);
?>


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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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.