Reading and Writing files in PHP

Source: Internet
Author: User

Introduction: This is a detailed page for PHP to read and write files. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 359984 'rolling = 'no'>

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:

 
Fopen (filename, Mode)

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

Mode Description
"R" Open in read-only mode. Point the file pointer to the beginning of the file.
"R +" Open in read/write mode. Point the file pointer to the beginning of the file.
"W" Open in writing 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.
"W +" Open in 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.
"" Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try 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, 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:

 
<? 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:

 
<? 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:

 
<? 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:

 
<? PHP $ fp = fopen ("test.txt", "R"); if ($ FP) {for ($ I = 1 ;! Feof ($ FP); $ I ++) {echo "line ". $ I. ":". fgets ($ FP ). "<br/>" ;}} else {echo "file opening failed";} fclose ($ FP);?>

Assume that the content of test.txt is:

 
Hello worldhello cnblogshello heihaozihello 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:

<? PHP $ fp = fopen ("test.txt", "W"); // After the file is cleared, write if ($ FP) {$ COUNT = 0; for ($ I = 1; $ I <= 5; $ I ++) {$ flag = fwrite ($ FP, "row ". $ I. ":". "Hello world! \ R \ n "); If (! $ Flag) {echo "file writing failed <br>"; break;} $ count + = $ flag;} echo "write in total ". $ count. "characters";} else {echo "file opening failed";} 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.

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/359984.html pageno: 1.

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.