PHP csv文件导入数据

Jackey PHP 2,910 次浏览 , , 没有评论
  1. $file=$_FILES['file'];
  2. $fileName=$file['tmp_name'];
  3. if (!$files = fopen($fileName, 'r')) {
  4. echo "文件读取失败";
  5. }
  6. $data = [];
  7. fgetcsv($files); // skip table head
  8. while (!feof($files)) {
  9. $line = fgetcsv($files);
  10. if (is_array($line)){
  11. foreach ($line as $key => $value){
  12. $encode = mb_detect_encoding($value, array('UTF-8','GB2312','GBK'));
  13. if ($encode != 'UTF-8'){
  14. $line[$key] = iconv('gb2312', 'utf-8', $value);
  15. }else{
  16. $line[$key] = $value;
  17. }
  18. }
  19. $data[] = $line;
  20. }
  21. }
  22. if (empty($data)) {
  23. echo "文件内容为空";
  24. }
  25.  
  26. // TODO $data 为导入后的数据,可做业务处理

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Go