Notepad++Good Luck To You!

php 读取csv文件并保存到mysql数据库
<?php
function readFile()
{
   //用为用的是生成器,所以不用担心csv文件的大小
   $handle = fopen(WEB_ROOT . "abc.csv", 'rb');
   while (feof($handle) === false) {
       yield fgetcsv($handle);
   }
   fclose($handle);
}


set_time_limit(0);

#ini_set('memory_limit', '50M');
header("content-type:text/html;charset=utf-8");

$insert_data = [];
foreach ($this->readFile() as $k => $v) {
   if ($k >= 2 && is_array($v)) {
       $v = self::convert_arr($v);

       //print_r($v);die;

       $v1 = strval($v[1]);
       if ($v1 == '0') {
           $parent_id = 0;
           $path = 0;
       } else {
           $demo_arr = explode('_', $v1);
           $parent_id = $demo_arr[0];
           $path = implode('-', $demo_arr);
       }
       if (empty($v[4])) {
           continue;
       }

       $insert_data = [

           'id' => $v[0],
           'parent_id' => $parent_id,
           'path' => $path,
           //手机号
           'mobile' => $v[4],
           //昵称
           'nickname' => $v[21],
           //真实姓名
           'realname' => $v[22],
           //注册时间
           'create_time' => $v[45],
           'pdd_pid' => $v[30],
           'jd_pid' => $v[29],
           'relation_id' => $v[70],
           'number' => getRandNumber(6),
           'unionid' => $v[24],
           //头像
           'avatar' => $v[32],

       ];

       Db::name("members")->insert($insert_data);
       unset($insert_data);

   }
}
//编码转换
function convert_arr($arr)
{
   return array_map(function ($v) {
       return mb_convert_encoding($v, 'utf-8', 'gbk');
   }, $arr);
}


«    2023年7月    »
12
3456789
10111213141516
17181920212223
24252627282930
31
TOP 搜索
TOP 控制面板
您好,欢迎到访网站!
  查看权限
TOP 最新留言
    TOP 作者列表
    TOP 站点信息
    • 文章总数:163
    • 页面总数:0
    • 分类总数:6
    • 标签总数:20
    • 评论总数:0
    • 浏览总数:312004
    召唤伊斯特瓦尔