Home >Backend Development >PHP Tutorial >Reading and writing files in PHP_PHP tutorial
There are two more practical functions for reading and writing files in php, fopen and fwrite. With these two functions, you can operate files very well. If you need it, you can read the detailed explanation.
1.fopen (create files and open files)
Syntax:
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" opens in read-only mode, pointing the file pointer to the beginning of the file.
"r+" opens in read-write mode and points 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+" opens in read-write 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.
"a" opens in writing mode and points the file pointer to the end of the file. If the file does not exist then attempts to create it.
"a+" opens in read-write mode and points 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 | Copy code |
代码如下 | 复制代码 |
$fp = fopen("test.txt", "r"); ?> |
?>
2.fclose (close file)
Syntax:
filepointer, the file pointer to be closed. The fclose function returns TRUE if successful and FALSE if failed.
代码如下 | 复制代码 |
$fp = fopen("test.txt", "r"); fclose($fp); ?> |
The code is as follows | Copy code |
$fp = fopen("test.txt", "r"); fclose($fp); ?>
|
Syntax:
代码如下 | 复制代码 |
$fp = fopen("test.txt", "r"); while(! feof($fp)) { echo fgets($fp). " "; } fclose($fp); ?> |
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 | Copy code | ||||
$fp = fopen("test.txt", "r"); while(! feof($fp)) {
echo fgets($fp). "
|
The code is as follows | Copy code |
$fp = fopen("test.txt", "r");<🎜>
if($fp)<🎜>
{<🎜>
for($i=1;! feof($fp);$i++)<🎜>
{<🎜>
echo "OK".$i." : ".fgets($fp). " "; } } else { echo "Failed to open file"; } fclose($fp); ?> |
Suppose 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 everyone5.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
|
Copy code
|
||||
$fp = fopen("test.txt", "w");//The file is cleared and then written if($fp) {
$count=0; "; Break; } |
$count+=$flag;
else
Line 1: Hello World! Line 2: Hello World!