分享下php讀取檔案內容的三種方法。
php讀取檔案內容:
- //**************第一種讀取方式*****************************
- header("content-type:text/html;charset=utf-8");
- //檔案路徑
- $file_path="text.txt";
- //判斷是否有這個檔案
- if(file_exists($file_path)){
- if($fp=fopen($file_path,"a+")){
- //讀取檔案
- $c>fread($fp,filesize($file_path));
- //替換字串
- $c>str_replace("\r\n","
",$conn);
- echo $conn."
";
- }else{
- echo "檔案打不開";
- }
- }else{
- echo "沒有這個檔案";
- }
- fclose($fp);
-
-
- //*******************第二種讀取方式***************************
- header("content-type:text/html;charset=utf-8");
- //檔案路徑
- $file_path="text.txt";
- $c>file_get_contents($file_path);
- $c>str_replace("\r\n","
",file_get_contents($file_path));
- echo $conn;
- fclose($fp);
-
-
- //******************第三種讀取方式,迴圈讀取*****************
- header("content-type:text/html;charset=utf-8");
- //檔案路徑
- $file_path="text.txt";
- //判斷檔案是否存在
- if(file_exists($file_path)){
- //判斷檔案是否能開啟
- if($fp=fopen($file_path,"a+")){
- $buffer=1024;
- //邊讀邊判斷是否到了檔案末尾
- $str="";
- while(!feof($fp)){
- $str.=fread($fp,$buffer);
- }
- }else{
- echo "檔案不能開啟";
- }
- }else{
- echo "沒有這個檔案";
- }
- //替換字元
- $str=str_replace("\r\n","
",$str);
- echo $str;
- fclose($fp);
- 讀取INI設定檔的函數:
- $arr=parse_ini_file("config.ini");
- //返回的是數組
- echo $arr['host']."
";
- echo $arr['username']."
";
- echo $arr['password']."
";
以上就介紹了php讀取檔案內容的三種方式轉,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。