当前位置: 代码迷 >> 综合 >> PHP大文件上传(csv/xlsx)
  详细解决方案

PHP大文件上传(csv/xlsx)

热度:61   发布时间:2023-09-19 16:42:44.0

cvs读取

function get_svg($files){$file = fopen($files,'r'); if (!$file) {return $data['error'] = 1;}while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容 $goods_list[] = eval('return '.iconv('gbk','utf-8',var_export($data,true)).';');}$data['data'] = $goods_list;fclose($file); return $data;}

xlsx读取

function excel_to_mysql($file){ob_clean();//导入PHPExcel第三方类库//vendor('PHPExcel.PHPExcel');import('Classes.PHPExcel',COMMON_PATH,'.php');//实例化PHPExcel类,用于接收Excel文件       $PHPExcel = new PHPExcel();//读取Excel文件类实例化$PHPReader = new PHPExcel_Reader_Excel2007();//检测Excel版本是否可读if(!$PHPReader->canRead($file)){$PHPReader = new PHPExcel_reader_Excel5();if(!$PHPReader->canRead($file)){return array('error'=>1);//未知版本的Excel} }//读取Excel文件$PHPExcel = $PHPReader->load($file,$encode="utf8");//获得Excel中表的数量$sheetCount = $PHPExcel->getSheetCount();//获得第一张工作表$sheet=$PHPExcel->getSheet(0);//获得表中最大数据列名$column = $sheet->getHighestColumn();//获得表中最大数据行名$row = $sheet->getHighestRow();$column = PHPExcel_Cell::columnIndexFromString($column); $data=array();for($j=1;$j<=$row;$j++){  for($g=0;$g<$column;$g++){$datas = (string)$sheet->getCellByColumnAndRow($g,$j)->getValue();if ($datas) {$data[$j][] = $datas;}}}//释放工作表对象unset($sheet);//释放读取Excel文件对象unset($PHPReader);//释放Excel文件对象unset($PHPExcel);//返回数据return array('error'=>0,'data'=>$data);}

插入数据库前的判断

foreach ($list as $k => $v) {if($k==0){continue;}if(!$v[1] || !preg_match("/^1[3|4|5|6|9|7|8]\d{9}$/",$v[1]) && !preg_match("/^(0[0-9]{2,3})?([2-9][0-9]{6,7})$/",$v[1])){continue;}//用户$arr[] = array('eid'=>trim($v[0]),'mobile'=>trim($v[1]),'key'=>trim($v[2]),'area'=>trim($v[3]),'userid'=>$_SESSION['admin']['userid'],'addtime'=>time());$count_arr = count($arr);if ( $count_arr >= 1000 || $k == $count ) {M('user')->addAll($arr);//每一千条,插入一次M('num')->where("sub_id=%d and type = 3",$_SESSION['admin']['userid'])->setInc('num',$count_arr);}
}

 

  相关解决方案