欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php讀取文件內(nèi)容的方法匯總

 更新時間:2015年01月24日 12:08:55   投稿:shichen2014  
這篇文章主要介紹了php讀取文件內(nèi)容的方法,實例匯總了常見的五種方法,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例匯總了php讀取文件內(nèi)容的方法。分享給大家供大家參考。具體如下:

這里匯總php讀取文件內(nèi)容的五種方法。實際應(yīng)用當中,請注意關(guān)閉 fclose($fp);

第一種方法:fread()

復制代碼 代碼如下:
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
 $fp = fopen($file_path,"r");
 $str = fread($fp,filesize($file_path));//指定讀取大小,這里把整個文件內(nèi)容讀取出來
 echo $str = str_replace("\r\n","<br />",$str);
}
?>

第二種方法:
復制代碼 代碼如下:
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
 $str = file_get_contents($file_path);//將整個文件內(nèi)容讀入到一個字符串中
 $str = str_replace("\r\n","<br />",$str);
 echo $str;
}
?>

第三種方法:
復制代碼 代碼如下:
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
 $fp = fopen($file_path,"r");
 $str = "";
 $buffer = 1024;//每次讀取 1024 字節(jié)
 while(!feof($fp)){//循環(huán)讀取,直至讀取完整個文件
  $str .= fread($fp,$buffer);
 }
 $str = str_replace("\r\n","<br />",$str);
 echo $str;
}
?>

第四種方法:
復制代碼 代碼如下:
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
 $file_arr = file($file_path);
 for($i=0;$i<count($file_arr);$i++){//逐行讀取文件內(nèi)容
  echo $file_arr[$i]."<br />";
 }
 /*
 foreach($file_arr as $value){
  echo $value."<br />";
 }*/
}
?>

第五種方法:
復制代碼 代碼如下:
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
 $fp = fopen($file_path,"r");
 $str ="";
 while(!feof($fp)){
  $str .= fgets($fp);//逐行讀取。如果fgets不寫length參數(shù),默認是讀取1k。
 }
 $str = str_replace("\r\n","<br />",$str);
 echo $str;
}
?>

希望本文所述對大家的php程序設(shè)計有所幫助。

相關(guān)文章

最新評論