Home >Backend Development >PHP Tutorial >Summary of PHP file reading methods and detailed explanation of operation codes

Summary of PHP file reading methods and detailed explanation of operation codes

伊谢尔伦
伊谢尔伦Original
2017-07-17 09:59:321108browse

In PHP website development, there are usually two ways to store data. One is stored in text files, such as txt files, and the other is stored in databases, such as Mysql. Compared with database storage, file storage There is no advantage, but file read and write operations are still used from time to time in basic PHP development

The operation of writing data to a file mainly involves three steps and some file operation functions are as follows:
1. Open the file (file operation function: fopen)
2. Write to the file (file operation function: fwrite, etc.)
3. Close the file (file operation function: fclose)
Next, read and write through the file Operation code example tutorial to explain
Basic PHP file writing operation functions fopen, fwrite, fclose application tutorial

<? 
@$fp = fopen("leapsoulcn.txt","w"); 
if(!$fp){ 
echo "system error"; 
exit(); 
}else { 
$fileData = "domain"."\t"."www.php.cn"."\n"; 
$fileData = $fileData."description"."\t"."PHP网站,面向PHP初学者的
PHP教程
网。"."\n"; 
$fileData = $fileData."title"."\t"."本文主要讲述PHP文件读写操作中最基本的文件写入教程。"; 
fwrite($fp,$fileData); 
fclose($fp); 
} 
?>

Note: In this file reading and writing operation example code, The main function is to write two lines of text in the file.
Knowledge points:
1. Use the fopen function to open a file. When applying the fopen function to prepare to open a file, you first need to clarify:
Why open the file? Is it reading the data in the file, writing the data to the file, or reading and writing the file?
In addition, you need to consider if relevant data already exists in the file, should you overwrite the data in the original file, or just add new data to the end of the file
These issues involve fopen in PHP file read and write operations For the application of file mode in the function, the fopen function prototype is as follows:

fopen(filename,mode,include_path,context)

When calling the file operation function fopen(), you usually need to pass two or three parameters.
Filename: Specifies the file or URL to be opened. You can specify the absolute path of the file, which is generally C:\ for Windows and / for Unix. You can also open remote files through the URL. The file written here is in the same directory as the PHP file writing code file I placed.
Mode: Specifies the type of access required to the file/stream. That is, the mode in which the file is opened.
 include_path: optional. If you need to search files in include_path, you can set this parameter to 1 or TRUE.
 Commonly used fopen file operation mode description
 "r" - Open the file in read-only mode and start reading from the file header.
 "r+" - Open the file for reading and writing.
 "w" - Open the file for writing and start writing from the beginning of the file. If the file does not exist, try to create it. If the file exists, delete the contents of the file first.
 "w+" - Open the file for reading and writing, starting from the beginning of the file. If the file does not exist, try to create it. If the file exists, delete the contents of the file first.
 "a" - Open for writing, append writing starting from the end of the file. If the file does not exist then attempts to create it.
 "a+" - Open in read-write mode, append writing or reading starting from the end of the file. If the file does not exist then attempts to create it.
Note: When performing file read and write operations, you must ensure that the opened file has the corresponding read and write permissions, otherwise fopen will report an error. You can use @ to suppress errors and handle them appropriately.
 2. After using the file operation function fopen to open the file, you need to assign a value to the variable and then write it to the file pointer pointed to by $fp. In the above example of the PHP file writing operation tutorial, I used line by line. Storage, that is, newline storage, mainly uses \n as the newline separator.
 The fwrite file writing function prototype is as follows:

The code is as follows:

fwrite(fp,string,length)

Here you can also use the file writing function fputs, which is an alias of fwrite Function, function and usage are the same as fwrite.
Length is an optional option in the file writing function fwrite. It is mainly used to set the maximum number of characters to be written to the file. If this parameter is set, fwrite will write the specified file in the specified file according to the set length. length of characters. fwrite() returns the number of characters written to the file, or false if an error occurs.
After the file writing operation is completed, the file handle needs to be closed, otherwise it will occupy system resources, etc. You can use the fclose($fp) function to accomplish this. Returns true if the file is closed successfully, otherwise returns false.
At this point, the file writing operation is completed.

Here are some ways to read files:

1. fread()

<?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);
}
?>

2.

<?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;
}
?>

3.

<?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;
}
?>

4.

<?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 />";
}*/
}
?>

5.

<?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;
}
?>

The above is the detailed content of Summary of PHP file reading methods and detailed explanation of operation codes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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