Home > Backend Development > PHP Tutorial > fopen()函数 r r+到底有啥区别啊?

fopen()函数 r r+到底有啥区别啊?

WBOY
Release: 2016-06-23 13:41:05
Original
2472 people have browsed it

fopen(),后面的参数,
比如:r  
r+
一个只读模式,一个读写模式,到底有什么区别啊,为什么要创造出个后面这个模式?
能不能通过一个实例让我明白下?


回复讨论(解决方案)

r ,只能读取fread,不能写入fwrite
r+ ,两者都可以,且是覆盖写入

$fn = 'abc.txt'; //待操作的文件名file_put_contents($fn, '12345'); //写入测试数据readfile($fn); //看一下,内容为 12345//r 只读模式$fp = fopen($fn, 'r');$c = fgetc($fp);echo $c;echo fputs($fp, 'a'); //0 没有字符被写入fclose($fp);readfile($fn); //看一下,内容为 12345 没有改变//r+ 读写模式$fp = fopen($fn, 'r+');$c = fgetc($fp);echo $c;echo fputs($fp, 'a'); //1 写入1个字符fclose($fp);readfile($fn); //再看一下,内容为 1a345
Copy after login

r 只能读取里面的内容,没有权限往文件里面写
r+ 可以往里边写

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template