php讀取文件內(nèi)容的方法匯總
更新時間:2015年01月24日 12:08:55 投稿:shichen2014
這篇文章主要介紹了php讀取文件內(nèi)容的方法,實例匯總了常見的五種方法,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例匯總了php讀取文件內(nèi)容的方法。分享給大家供大家參考。具體如下:
這里匯總php讀取文件內(nèi)容的五種方法。實際應用當中,請注意關閉 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);
}
?>
$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;
}
?>
$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;
}
?>
$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 />";
}*/
}
?>
$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;
}
?>
$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程序設計有所幫助。
相關文章
php正則preg_replace_callback函數(shù)用法實例
這篇文章主要介紹了php正則preg_replace_callback函數(shù)用法,實例分析了preg_replace_callback函數(shù)進行正則替換的相關技巧,需要的朋友可以參考下2015-06-06
PHP使用preg_split和explode分割textarea存放內(nèi)容的方法分析
這篇文章主要介紹了PHP使用preg_split和explode分割textarea存放內(nèi)容的方法,結合實例形式分析preg_split和explode函數(shù)的功能、使用技巧與文本字符串分割過程中的相關注意事項,需要的朋友可以參考下2017-07-07
php+mysql實現(xiàn)的無限分類方法類定義與使用示例
這篇文章主要介紹了php+mysql實現(xiàn)的無限分類方法類定義與使用,結合實例形式分析了php+mysql無限分類方法類基本定義與使用方法,需要的朋友可以參考下2020-05-05
php中修改瀏覽器的User-Agent來偽裝你的瀏覽器和操作系統(tǒng)
修改瀏覽器的User-Agent來偽裝你的瀏覽器和操作系統(tǒng),這兩種都可以得到User-Agents和IP等信息,最好配合正則表達式,對信息進行篩選剔除。2011-07-07

