To read and write files in PHP, you can use the built-in functions:
1.fopen (create file and open file)
Grammar:
Copy CodeThe code is as follows: fopen (Filename,mode)
FileName, which specifies the file to open. mode, open the file's schema, the possible values are shown in the table below.
Mode description
The "R" read-only opens, pointing the file pointer to the beginning of the file.
The "r+" read-write mode opens, pointing the file pointer to the beginning of the file.
The "W" Write method opens, pointing the file pointer at the beginning of the file and truncating the file size to zero. If the file does not exist, try to create it.
The "w+" read-write mode opens, pointing the file pointer at the beginning of the file and truncating the file size to zero. If the file does not exist, try to create it.
The "A" write opens, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
A + read-write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
If the file is opened successfully, the return value of the fopen function is a file pointer and returns FALSE if an error occurs.
Example:
Copy CodeThe code is as follows:
$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 if the Fclose function returns FALSE if it fails.
Example:
Copy CodeThe code is as follows:
$fp = fopen ("Test.txt", "R");
Fclose ($FP);
?>
3.feof (detects if the end of file has been reached)
Grammar:
Feof (Filepointer)
Filepointer, to detect a file pointer, the pointer must point to a file that was successfully opened without closing. The feof function returns TRUE if the file pointer is to the end of the file or if an error occurs.
Example:
Copy CodeThe code is as follows:
$fp = fopen ("Test.txt", "R");
while (! feof ($FP))
{
Echo fgets ($FP). "
";
}
Fclose ($FP);
?>
4.fgets (reads a row from the file pointer)
Grammar:
Fgets (Filepointer)
Filepointer, the file pointer to be read. If successful, reads a row from the file and returns a string, FALSE if it fails.
Example:
Copy CodeThe code is as follows:
$fp = fopen ("Test.txt", "R");
if ($FP)
{
For ($i =1;! feof ($fp); $i + +)
{
echo "line". $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 result of the page output is:
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. String that is to be written. If successful, returns the number of characters written, or FALSE if it fails.
Example:
Copy CodeThe code is as follows:
$fp = fopen ("Test.txt", "w");//file is emptied and then written
if ($FP)
{
$count = 0;
for ($i =1; $i <=5; $i + +)
{
$flag =fwrite ($fp, "line". $i. ":". " Hello world!\r\n ");
if (! $flag)
{
echo "Failed to write to file
";
Break
}
$count + = $flag;
}
echo "Total write". $count. " Characters ";
}
Else
{
echo "Failed to open file";
}
Fclose ($FP);
?>
The result of the page output is:
Write 100 characters in total
The Test.txt file 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: For ease of operation, optional parameters for some functions are not listed.
http://www.bkjia.com/PHPjc/324337.html www.bkjia.com true http://www.bkjia.com/PHPjc/324337.html techarticle read and write files in PHP, you can use the built-in functions: 1.fopen (Create files and open files) syntax: Copy code code is as follows: fopen (filename,mode) filename, rules to open ...