To read and write files in PHP, you can use the built-in functions:
1.fopen (create file and open file)
Grammar:
Copy code code as follows: fopen (Filename,mode)
filename, specify the file to open. mode, open the pattern of the file, the possible values are shown in the following table.
Mode description
"R" opens in read-only mode, pointing the file pointer at the beginning of the file.
The "r+" read-write mode opens, pointing the file pointer at the beginning of the file.
The "W" write mode opens, pointing the file pointer at the beginning of the file and truncating the file size to zero. Try to create if the file does not exist.
The "w+" read-write mode opens, pointing the file pointer at the beginning of the file and truncating the file size to zero. Try to create if the file does not exist.
The "A" write is opened, pointing the file pointer at the end of the file. Try to create if the file does not exist.
A + read-write mode opens, pointing the file pointer to the end of the file. Try to create if the file does not exist.
If the file is successfully opened, the return value of the fopen function is a file pointer and, if an error occurs, returns FALSE.
Example:
The code is as follows:
<?php
$fp = fopen ("Test.txt", "R");
? >
2.fclose (Close file)
Grammar:
Fclose (Filepointer)
Filepointer, the file pointer to close. If successful, the Fclose function returns TRUE, and if it fails, the Fclose function returns FALSE.
Example:
The code is as follows:
<?php
$fp = fopen ("Test.txt", "R");
Fclose ($FP);
3.feof (detects whether the end of the file has been reached)
Grammar:
Feof (Filepointer)
Filepointer, the file pointer to be instrumented, which must point to a successful open file that is not closed. The feof function returns TRUE if the file pointer is at the end of the file or if an error occurs.
Example:
The code is as follows:
<?php
$fp = fopen ("Test.txt", "R");
while (! feof ($fp))
{
echo fgets ($fp). "<br/>";
}
Fclose ($FP);
4.fgets (read one row from the file pointer)
Grammar:
Fgets (Filepointer)
Filepointer, the file pointer to read. If successful, reads a row from the file and returns a string, FALSE if it fails.
Example:
The code is as follows:
<?php
$fp = fopen ("Test.txt", "R");
if ($fp)
{for
($i =1;! feof ($fp), $i + +)
{
echo "line". $i. ":". Fgets ($FP). " <br/> ";
}
}
else
{
echo "open file Failed";
}
Fclose ($FP);
Suppose the contents of the Test.txt are:
Hello World
Hello Cnblogs
Hello Heihaozi
Hello everyone
The results of the page output are:
Line 1:hello World
Line 2:hello Cnblogs
Line 3:hello Heihaozi
Line 4:hello Everyone
5.fwrite (write file)
Grammar:
Fwrite (filepointer,string)
Filepointer, the file pointer to write to. String that is to be written. If successful, returns the number of characters written and, if it fails, returns FALSE.
Example:
The code is as follows:
<?php
$fp = fopen ("Test.txt", "w");//file is emptied and then written to
if ($fp)
{
$count =0;
For ($i =1 $i <=5; $i + +)
{
$flag =fwrite ($fp, line. $i. ":". " Hello world!\r\n ");
if (! $flag)
{
echo write file failed <br>;
break;
$count + + $flag;
}
echo "Altogether writes". $count. " Characters ";
}
else
{
echo "open file Failed";
}
Fclose ($FP);
The results of the page output are:
100 characters written in a total
Test.txt files will be written to:
Line 1:hello world!
Line 2:hello world!
Line 3:hello world!
Line 4:hello world!
Line 5:hello world!
Note: The optional parameters for some functions are not listed to simplify the operation.