Summary of PHP file operation methods
This article mainly introduces relevant information about the summary of PHP file operation methods. Friends in need can refer to it
Write data in the data file:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* 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');//打开文件
fwrite($f,'Hello PHP');//写入数据
fclose($f);//关闭文件
echo 'OK';
//windows环境暂时不考虑权限问题
|
1
2
3
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* 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);
|
5
6
7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/29
* Time: 17:05
*/
header("Content-type: text/html; charset=utf-8");
$f = fopen('data','r');
//读取多行数据 while
while(!feof($f)){//feof() 函数检测是否已到达文件末尾
$content = fgets($f);
echo $content;
}
fclose($f);
|
8
9
10
1
|
echo file_get_contents('data');
|
11
12
13
14
|
/**
* 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 file
fwrite($f,'Hello PHP');//Write data
fclose($f);//Close the file
echo 'OK';
//Windows environment does not consider permission issues for the time being
|
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
|
|
How to read if there are multiple rows of data?
Method 1 while:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
|
Method 2 file_get_contents():
?
1
|
echo file_get_contents('data');
|
The above is the entire content of this article, I hope you all like it.
http://www.bkjia.com/PHPjc/1025316.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1025316.htmlTechArticleA summary of PHP file operation methods. This article mainly introduces relevant information about a summary of PHP file operation methods. Friends who need it can Write data in the data file as a reference: ? 1 2 3 4 5 6 7 8 9...