Home > Backend Development > PHP Tutorial > Five ways to read file content in PHP, _PHP tutorial

Five ways to read file content in PHP, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:18
Original
1029 people have browsed it

Five ways to read file content in PHP,

Five ways to read file content in PHP

Share five ways to read file contents in PHP: Well, after writing, I found that all the files were not closed. In actual application, please pay attention to closing fclose($fp);
--

php reads file content:

-----First method-----fread()--------

<&#63;php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
echo $str = str_replace("\r\n","<br />",$str);
}
&#63;>

Copy after login

--------The second method------------

<&#63;php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
&#63;>
Copy after login

-----Third method------------

<&#63;php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次读取 1024 字节
while(!feof($fp)){//循环读取,直至读取完整个文件
$str .= fread($fp,$buffer);
} 
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
&#63;>
Copy after login

-------The fourth method--------------

<&#63;php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
&#63;>
Copy after login

----The fifth method--------------------

<&#63;php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
&#63;>
Copy after login

The above content has shared with you five ways to read file content in PHP. I hope you like it.

Articles you may be interested in:

  • PHP code for reading and modifying a certain line of content in a large file
  • PHP code for reading file content (txt, js, etc.)
  • PHP implementation code for reading the content of web page files (fopen, curl, etc.)
  • PHP code for reading the content of a txt file and assigning it to an array
  • php reading the file Content into a string, while removing newlines, blank lines, and spaces at the beginning and end of lines (original by Zjmainstay)
  • Detailed explanation of reading csv file content based on PHP
  • PHP X for reading large files Implementation code for the content from line to line Y
  • Detailed explanation of several methods for php to read the file content
  • PHP clears the file after reading the file content Sample code
  • php reads the file Summary of content methods
  • PHP method of reading file content into an array

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1085892.htmlTechArticleFive ways to read file content in PHP. Five ways to read file content in PHP. Share the five ways to read file content in PHP. Five methods for file content: Well, after writing, I found that all the files were not closed. Actual...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template