讀取文件內(nèi)容的五種方式
分享下php讀取文件內(nèi)容的五種方法:好吧,寫完后發(fā)現(xiàn)文件全部沒有關(guān)閉 。實(shí)際應(yīng)用當(dāng)中 , 請注意關(guān)閉 fclose($fp);
—
php讀取文件內(nèi)容:
—–第一種方法—–fread()——–
<?php
$file_path = “test.txt”;
if(file_exists($file_path)){
$fp = fopen($file_path,”r”);
$str = fread($fp,filesize($file_path));//指定讀取大小 , 這里把整個(gè)文件內(nèi)容讀取出來
echo $str = str_replace(“rn”,”<br />”,$str);
}
?>
——–第二種方法————
<?php
$file_path = “test.txt”;
【php獲取文件列表介紹 php讀取文件內(nèi)容并輸出】if(file_exists($file_path)){
$str = file_get_contents($file_path);//將整個(gè)文件內(nèi)容讀入到一個(gè)字符串中
$str = str_replace(“rn”,”<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)讀取 , 直至讀取完整個(gè)文件
$str .= fread($fp,$buffer);
}
$str = str_replace(“rn”,”<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ù),默認(rèn)是讀取1k 。
}
$str = str_replace(“rn”,”<br />”,$str);
echo $str;
}
?>
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時(shí)就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- Scrivener設(shè)置自動(dòng)備份為ZIP文件
- 如何在C語言中實(shí)現(xiàn)文件內(nèi)容讀取
- Photoshop CC 2017軟件打開及新建文件
- JavaScript在HTML文件中的三個(gè)編寫位置
- EasyEclipse:創(chuàng)建PHP項(xiàng)目的簡單步驟
- 泰拉瑞亞妖刀村正獲取方法詳解
- WIN7系統(tǒng)如何查看隱藏的文件和文件夾
- 提升文件安全性的WinRAR小技巧
- 如何快速將DWG文件轉(zhuǎn)為DXF文件
- 如何在UltraCompare中開啟Word文本文件
