国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP讀取文件內(nèi)容的五種方式

2019-11-06 07:24:33
字體:
供稿:網(wǎng)友

php讀取文件內(nèi)容的五種方式

分享下php讀取文件內(nèi)容的五種方法:好吧,寫完后發(fā)現(xiàn)文件全部沒有關(guān)閉。實際應(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));//指定讀取大小,這里把整個文件內(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;}?>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 尚义县| 武清区| 平利县| 奉节县| 邯郸市| 潮州市| 武陟县| 宕昌县| 秀山| 乳山市| 漯河市| 万州区| 丹江口市| 延川县| 清新县| 通化市| 泽州县| 昌吉市| 湖南省| 同心县| 平安县| 舒城县| 梓潼县| 新和县| 平泉县| 石家庄市| 阿拉善盟| 色达县| 延边| 得荣县| 黄平县| 陇川县| 虞城县| 通化市| 林芝县| 霍林郭勒市| 定襄县| 文化| 视频| 西青区| 榆林市|