PHP file operation method summary

Source: Internet
Author: User

PHP file operation method summary

This article mainly introduces the summary of PHP file operation methods. For more information, see

Write data into the data file:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<? Php

/**

* Created by PhpStorm.

* User: Administrator

* Date: 2015/6/29

* Time: 17: 05

*/

Header ("Content-type: text/html; charset = UTF-8 ");

// Write data

$ F = fopen ('data', 'w'); // open the file

Fwrite ($ f, 'Hello php'); // write data

Fclose ($ f); // close the file

Echo 'OK ';

// Permissions are not considered in windows.

After successful writing, you can see "OK" on the page"

Next, read the data in the data file.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<? Php

/**

* Created by PhpStorm.

* User: Administrator

* Date: 2015/6/29

* Time: 17: 05

*/

Header ("Content-type: text/html; charset = UTF-8 ");

 

// Read data

$ F = fopen ('data', 'R ');

$ Content = fgets ($ f );

Echo $ content;

Fclose ($ f );

How can I read multi-row data?

Method 1 while:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<? Php

/**

* Created by PhpStorm.

* User: Administrator

* Date: 2015/6/29

* Time: 17: 05

*/

Header ("Content-type: text/html; charset = UTF-8 ");

$ F = fopen ('data', 'R ');

// Read multiple rows of data while

While (! Feof ($ f) {// feof () function checks whether it has reached the end of the file

$ Content = fgets ($ f );

Echo $ content;

}

Fclose ($ f );

Method 2: file_get_contents ():

?

1

Echo file_get_contents ('data ');

The above is all the content of this article. I hope you will like it.

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.