Home  >  Article  >  Backend Development  >  How to read and write files in php?

How to read and write files in php?

怪我咯
怪我咯Original
2017-07-07 09:26:304028browse

The code for reading and writing files in PHP is well organized. Friends who need it can refer to it.

To read and write files in PHP, you can use the following built-in functions:

1.fopen (create files and open files)
Syntax:

The code is as follows:

fopen(filename,mode)

filename, specifies the file to be opened. mode, the mode for opening the file. Possible values ​​are shown in the table below.

mode Description

"r" Open in read-only mode, pointing the file pointer to the beginning of the file.
"r+" Open in reading and writing mode and point the file pointer to the beginning of the file.
"w" turns on writing mode, points the file pointer to the beginning of the file and truncates the file size to zero. If the file does not exist then attempts to create it.
"w+" Open in reading and writing mode, point the file pointer to the beginning of the file and truncate the file size to zero. If the file does not exist then attempts to create it.
"a" Open in writing mode and point the file pointer to the end of the file. If the file does not exist then attempts to create it.
"a+" Open in read-write mode and point the file pointer to the end of the file. If the file does not exist then attempts to create it.
If the file is successfully opened, the return value of the fopen function is a file pointer. If an error occurs, FALSE is returned.

Example:

The code is as follows:

<?php 
$fp = fopen("test.txt", "r"); 
?>

2.fclose(Close the file)
Syntax:

fclose(filepointer)
filepointer, the file pointer to be closed. The fclose function returns TRUE if successful and FALSE if failed.
Example:

The code is as follows:

<?php 
$fp = fopen("test.txt", "r"); 
fclose($fp); 
?>

3.feof (check whether the end of the file has been reached)
Syntax:

feof(filepointer)
filepointer, the file pointer to be detected, which must point to a file that was successfully opened but not closed. If the file pointer reaches the end of the file or an error occurs, the feof function returns TRUE.
Example:

The code is as follows:

<?php 
$fp = fopen("test.txt", "r"); 
while(! feof($fp)) 
{ 
echo fgets($fp). "<br />"; 
} 
fclose($fp); 
?>

4.fgets (read a line from the file pointer)
Syntax:

fgets(filepointer )
filepointer, the file pointer to be read. If successful, read a line from the file and return a string, if failed, return FALSE.
Example:

The code is as follows:

<?php 
$fp = fopen("test.txt", "r"); 
if($fp) 
{ 
for($i=1;! feof($fp);$i++) 
{ 
echo "行".$i." : ".fgets($fp). "<br />"; 
} 
} 
else 
{ 
echo "打开文件失败"; 
} 
fclose($fp); 
?>

Assume that the content of test.txt is:

hello world
hello cnblogs
hello heihaozi
hello everyone
The page output result is:

Line 1: hello world
Line 2: hello cnblogs
Line 3: hello heihaozi
Line 4: hello everyone
5.fwrite (write file)
Syntax:

fwrite(filepointer,string)
filepointer, the file pointer to be written. string, the string to be written. Returns the number of characters written if successful, or FALSE if failed.
Example:

The code is as follows:

<?php 
$fp = fopen("test.txt", "w");//文件被清空后再写入 
if($fp) 
{ 
$count=0; 
for($i=1;$i<=5;$i++) 
{ 
$flag=fwrite($fp,"行".$i." : "."Hello World!\r\n"); 
if(!$flag) 
{ 
echo "写入文件失败<br>"; 
break; 
} 
$count+=$flag; 
} 
echo "共写入".$count."个字符"; 
} 
else 
{ 
echo "打开文件失败"; 
} 
fclose($fp); 
?>

The result of the page output is:

Write a total of 100 characters
test.txt file will Is written:

Line 1: Hello World!
Line 2: Hello World!
Line 3: Hello World!
Line 4: Hello World!
Line 5: Hello World!
 
Note: In order to simplify the operation, the optional parameters of some functions are not listed.

The above is the detailed content of How to read and write files in php?. 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