php中 fogets读取文件

<?php

header('content-type:text/html;charset=utf-8');

$file ="1.txt";

if(!file_exists($file)){
	die('文件不存在');
} //检测文件是否存在

$str = file_get_contents($file);

// $str =iconv('gb2312', 'utf-8', $str);

$encoding =mb_detect_encoding($str,$array = array("ASCII","utf-8","GB2312","GBK","BIG5"));

if($encoding !='utf-8'){
	
 $str =mb_convert_encoding($str, 'utf-8','gb2312');
}
$str =nl2br($str);
echo $str;

11111

<?PHP

$file ="student.csv";


if(!file_exists($file)) die('文件不存在');

//打开文件

$fp =@fopen($file,'r');

if(false ===$fp) die('打开文件失败');

fgets:从文件中读取一行 utf8编码格式

while($str =fgets($fp)){

$encoding =mb_detect_encoding($str,array("ASCII","utf-8","GB2312","GBK","BIG5"));

if($encoding !='utf-8'){
	  
 $str =mb_convert_encoding($str,'utf-8',$encoding);}
  // echo $str .'<br>';

//关闭文件

fclose($fp);

猜你喜欢

转载自blog.csdn.net/qq_41912774/article/details/84643296