文章提纲:
二.使用fopen方法打开文件
四.使用fclose方法关闭文件
五.文件指针的移动
六.windows和unix下的回车和换行
1.通过fopen方法打开文件:$fp =fopen(/*参数,参数*/),fp为resource类型
2.进行文件读取或者文件写入操作(这里使用的函数以1中返回的$fp作为参数)
3. 调用fclose($fp)关闭关闭文件
二:使用fopen方法打开文件
fopen(文件路径[string],打开模式[string])
<1>fopen的第一个参数为文件路径
写文件路径的方式:1绝对路径,2相对路径
1绝对路径:
在windows下工作的小伙伴们应该很熟悉,windows下的路径分隔符是“\\”而不是“/”,但我们在写入路径时不能以钦定的“\\”为分隔符
1
2
3
|
<?php
$fp = fopen ( "c:\\wamp64\\www\\text.txt" , 'w' );
?>
|
1
2
3
|
<?php
$fp = fopen ( "c:/wamp64/www/text.txt" , 'w' );
?>
|
运行时无报错,说明参数是有效的。
【注意】fopen函数不能理解“\\”分隔符,如果你想要使用“\\”,那么要使用转义,如写成:"c:\\\\wamp64\\\\www\\\\text.txt"这种写法也是可以的,函数也能理解,不会报错。但即使这样,也不推荐使用“\\”,因为在os(mac)下只能识别“/”不能识别“\\”
本小节的结论:推荐坚持使用“/”作为分隔符
2.相对路径:
上一小节介绍的是绝对路径的写法,但这样却带来了另外一个问题:服务器的目录结构可能会有较大的改变,这时原来写的绝对路径就要全部重写了,比如在我的电脑上的目标文件路径是c:/wamp64/www/text.txt,如果我把www文件夹改名为penghuwan呢?原来写入的路径参数就失效了。所以我们引入了相对路径的写法:
1
2
3
4
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'w' );
?>
|
• $_server是php的超级全局变量(在代码任何地方都可访问,类型是数组),通过$_server['document_root']可取到服务器的默认根目录
服务器的默认根目录可通过php.ini修改(这个可自行百度)
• $_server['document_root']在这里等同于c:/wamp64/www
本小节的结论:推荐使用相对路径
<2>fopen的第二个参数为打开模式
设置打开模式后,我们就相当于为接下来的读写操作设置了权限:
最基本的几个模式:
“r”:只能读取文件,不能写入文件(写入操作被忽略)
“w”:只能写入文件,不能读取文件(读取操作被忽略)
“a”:只追加文件,与“w”类似,区别是“w”删除原有的内容,“a”不删除原有内容,只追加内容
1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'w' );
fwrite( $fp , '在写模式下写入' );
fclose( $fp );
?>
|

1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r' );
fwrite( $fp , '在读模式下写入' );
fclose( $fp );
?>
|


很全面,但我觉得这张表对新手有些不太友好,让人看后不知多云。 r是只读,w是只写(原来有的内容全删除),a是追加(不删除原有内容),这都好理解。
1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r+' );
fwrite( $fp , 'r+ mode' );
fclose( $fp );
?>
|
1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'a+' );
fwrite( $fp , 'a+ mode' );
fclose( $fp );
?>
|
•采用w+模式写入文本“w+ mode”
1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'w+' );
fwrite( $fp , 'w+ mode' );
fclose( $fp );
?>
|

1
2
3
4
5
6
7
8
9
10
11
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r' ); //打开文件
if ( file_exists ( "$document_root/text.txt" )){ //当文件存在时,才读取内容
while (! feof ( $fp )){ //判断文件指针是否到达末尾
$c = fgetc ( $fp ); //每执行一次fgetc(),文件指针就向后移动一位
echo $c ; //输出获取到的字节
}
}
fclose( $fp ); //关闭文件
?>
|

1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r' );
echo fgetc ( $fp ); //只做一次输出
close( $fp );
?>
|

1
2
3
4
5
6
7
8
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r' );
echo fgetc ( $fp ); //连续做三次输出
echo fgetc ( $fp );
echo fgetc ( $fp );
fclose( $fp );
?>
|
2.一次读取多个字节 ——通过fread()方法:
1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r' );
echo fread ( $fp , 3); //一次输出三个字节即一个汉字字符(utf-8)
fclose( $fp );
?>
|
改成:
1
|
echo fread ( $fp , 6);
|

1
2
3
4
5
6
7
8
9
10
11
|
<?php
$document_root = $_server [ 'document_root' ]
$fp = fopen ( "$document_root/text.txt" , 'r' ); //打开文件
if ( file_exists ( "$document_root/text.txt" )){ //当文件存在时,才读取内容
while (! feof ( $fp )){ //判断文件指针是否到达末尾
$line = fgets ( $fp ); //返回一行文本,并将文件指针移动到下一行头部
echo $line . "<br/>" ; //输出获取到的一行文本
}
}
fclose( $fp ); //关闭文件
?>
|
1
|
$line = fgets ( $fp ,10);
|

1
2
3
4
5
6
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r' );
fpassthru ( $fp );
fclose( $fp );
?>
|

1
2
3
4
5
6
7
|
<?php
$document_root = $_server [ 'document_root' ];
$file_array = file( "$document_root/text.txt" ); //取到文件数组
foreach ( $file_array as $value ) { //输出数组元素
echo $value . "<br/>" ;
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php
$document_root = $_server [ 'document_root' ];
function print_file_pointer( $fp ){ //定义一个打印文件指针位置的函数
echo " <br/>//此时文件指针的位置:" ;
echo ftell ( $fp ). "<br/>" ;
}
$fp = fopen ( "$document_root/text.txt" , 'r' );
echo fgetc ( $fp ); //通过fgetc连续输出三个字节
echo fgetc ( $fp );
echo fgetc ( $fp );
print_file_pointer( $fp ); //打印此刻文件指针的位置
echo fread ( $fp ,6); //通过fread一次输出6字节
print_file_pointer( $fp ); //打印此刻文件指针的位置
echo fgets ( $fp ); //通过fgets输出一整行
print_file_pointer( $fp ); //打印此刻文件指针的位置
fpassthru ( $fp ); //一次性输出全部内容
print_file_pointer( $fp ); //打印此刻文件指针的位置
fseek ( $fp , 33); //使文件指针移动到33字节位置
print_file_pointer( $fp ); //打印此刻文件指针的位置
rewind ( $fp ); //使文件指针移动到0字节位置(初始位置)
print_file_pointer( $fp ); //打印此刻文件指针的位置
$fclose ( $fp );
?>
|

1
2
3
4
5
6
7
8
9
|
<?php
$document_root = $_server [ 'document_root' ];
$fp = fopen ( "$document_root/text.txt" , 'r' );
while (! feof ( $fp )){
echo fgets ( $fp );
echo ftell ( $fp );
}
fclose( $fp );
?>
|

相关文章
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 94
-
2025-05-29 89
-
2025-05-27 25
-
2025-05-29 70
-
2025-05-27 57