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.