PHP file read/write implementation code

Source: Internet
Author: User

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

1. fopen (create a file and open a file)
Syntax:Copy codeThe Code is as follows: fopen (filename, mode)

Filename, specifies the file to be opened. Mode. The possible values are shown in the following table.

Mode description
"R" is opened in read-only mode, pointing the file pointer to the beginning of the file.
Open the "r +" read/write mode and point the file pointer to the beginning of the file.
Open the "w" Write mode, point the file pointer to the beginning of the file, and cut the file size to zero. If the file does not exist, try to create it.
Open in "w +" read/write mode. Point the file pointer to the beginning of the file and cut the file size to zero. If the file does not exist, try to create it.
"A" is opened in writing mode, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
Open the "a +" read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
If the file is successfully opened, the fopen function returns a file pointer. If an error occurs, FALSE is returned.
Example:Copy codeThe Code is as follows: <? Php
$ Fp = fopen ("test.txt", "r ");
?>

2. fclose (close the file)
Syntax:

Fclose (filepointer)
Filepointer: the object pointer to be closed. If the operation succeeds, the fclose function returns TRUE. If the operation fails, the fclose function returns FALSE.
Example:Copy codeThe Code is as follows: <? Php
$ Fp = fopen ("test.txt", "r ");
Fclose ($ fp );
?>

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

Feof (filepointer)
Filepointer: The file pointer to be checked. The pointer must point to a file that is 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:Copy codeThe Code is as follows: <? Php
$ Fp = fopen ("test.txt", "r ");
While (! Feof ($ fp ))
{
Echo fgets ($ fp). "<br/> ";
}
Fclose ($ fp );
?>

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

Fgets (filepointer)
Filepointer: the object pointer to be read. If the operation succeeds, a row is read from the file and a string is returned. If the operation fails, FALSE is returned.
Example:Copy codeThe 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 "failed to open the file ";
}
Fclose ($ fp );
?>

Assume that the content of test.txt is:

Hello world
Hello cnblogs
Hello heihaozi
Hello everyone
The output result of the page is:

Row 1: hello world
Row 2: hello cnblogs
Row 3: hello heihaozi
Row 4: hello everyone
5. fwrite (Write File)
Syntax:

Fwrite (filepointer, string)
Filepointer: The file pointer to be written. String, the string to be written. If the write operation succeeds, the number of written characters is returned. If the operation fails, FALSE is returned.
Example:Copy codeThe Code is as follows: <? Php
$ Fp = fopen ("test.txt", "w"); // The file is cleared before being 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 File <br> ";
Break;
}
$ Count + = $ flag;
}
Echo "A total of". $ count. "characters ";
}
Else
{
Echo "failed to open the file ";
}
Fclose ($ fp );
?>

The output result of the page is:

Write 100 characters in total
The test.txt file will be written:

Row 1: Hello World!
Row 2: Hello World!
Row 3: Hello World!
Row 4: Hello World!
Row 5: Hello World!
  
Note: to simplify the operation, some function optional parameters are not listed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.