ThinkPHP5读取 txt文本文件数据

2019-09-20   阅读:4303   分类:后端    标签: PHP

ThinkPHP5读取 txt文本文件数据例子:

$file = request()->file('exfile');
	if($file){
		$info = $file->move(ROOT_PATH . 'public/static' . DS . 'uploads/txt');
		if($info){
			$exfile=ROOT_PATH.'/public/static/uploads/txt/'.date('Ymd').'/'.$info->getFilename();
			$filetxt = file_get_contents($exfile);  //文件路径
			$rep = str_replace("\r\n", ',', $filetxt); 
			$cont = explode(',', $rep);
		   for($i=0;$i<count($cont);$i++){
			   $data = [
				   'code' => $cont[$i],
				   'info_id' => 0,
			   ];
			   $result = db('info')->insert($data);
		   }
			   if($result){
				  return json(['code'=>200,'msg'=>'导入成功']);
				}
		}else{
			// 上传失败获取错误信息
			return json(['code'=>500,'msg'=>$file->getError()]);
		}
	}
}


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/220.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×