Home > Backend Development > PHP Tutorial > fopen()函数 r r+终竟有啥区别啊

fopen()函数 r r+终竟有啥区别啊

WBOY
Release: 2016-06-13 12:14:16
Original
1492 people have browsed it

fopen()函数 r r+到底有啥区别啊?
fopen(),后面的参数,
比如:r  
r+
一个只读模式,一个读写模式,到底有什么区别啊,为什么要创造出个后面这个模式?
能不能通过一个实例让我明白下?
------解决思路----------------------
r ,只能读取fread,不能写入fwrite
r+ ,两者都可以,且是覆盖写入
------解决思路----------------------

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

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

Related labels:
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