1. 程式人生 > >php中 fogets讀取檔案

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 [email protected]($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);