Maison > Article > développement back-end > PHP编程:介绍常见的文件操作方式_PHP教程
PHP处理本地服务器上的文件和目录是非常方便的,但有时候会出现权限和路径相关的问题。
1.打开文件
<ol class="dp-c"> <li class="alt"><span><span>resource </span><span class="func">fopen</span><span> ( string filename, string mode [, bool use_include_path [, resource zcontext]] ) </span></span></li> <li> <span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(filename,mode)</span><span class="comment">//打开文件,返回代表此文件的资源的句柄</span><span> </span> </li> </ol>
文件名称可以使用相对路径或者绝对路径也可以使用网络协议模式,打开模式具有rr+ww+aa+xx+b
在操作二进制文件时如果没有指定 'b' 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 rn 字符的奇怪问题。
为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 'b' 标记。
以下是几种打开文件的方式
<ol class="dp-c"> <li class="alt"><span><span class="vars">$fp</span><span> = @</span><span class="func">fopen</span><span>(</span><span class="string">'log.txt'</span><span>,</span><span class="string">"rb"</span><span>); </span></span></li> <li> <span class="vars">$fp</span><span> = @</span><span class="func">fopen</span><span>(</span><span class="string">'../log.txt'</span><span>,</span><span class="string">"rb"</span><span>); </span> </li> <li class="alt"> <span class="vars">$fp</span><span> = @</span><span class="func">fopen</span><span>(</span><span class="string">"http://www.runer.com.cn/default.htm"</span><span>,</span><span class="string">"rb"</span><span>);</span> </li> <li class="alt"><span>//还可以使用ftp和ghoper等协议,必须启用php.ini文件中的allow_url_fopen选项 </span></li> </ol>
<ol class="dp-c"> <li class="alt"><span><span class="vars">$filename1</span><span> = </span><span class="string">"userinfo.txt"</span><span>;</span><span class="comment">//目录下或include_path中存在这个文件 </span><span> </span></span></li> <li> <span class="vars">$filename2</span><span> = </span><span class="string">"test.txt"</span><span>;</span><span class="comment">//目录下或include_path并不存在这个文件 </span><span> </span> </li> <li class="alt"> <span class="vars">$resource1</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$filename1</span><span>,</span><span class="string">"rb"</span><span>); </span> </li> <li> <span>@</span><span class="vars">$resource2</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$filename2</span><span>,</span><span class="string">"rb"</span><span>);</span><span class="comment">//因为目录中不存在这个文件,并且并未使用或include_path寻找包含文件所在路径则此操作会报错,使用错误抑制符@可以迫使浏览器不输出错误信息 </span><span> </span> </li> <li class="alt"> <span class="keyword">if</span><span>(</span><span class="vars">$resource1</span><span>) </span> </li> <li> <span class="func">echo</span><span> </span><span class="string">"打开文件{$filename1}成功"</span><span>; </span> </li> <li class="alt"> <span class="keyword">if</span><span>(!@</span><span class="func">fopen</span><span>(</span><span class="vars">$filename2</span><span>,</span><span class="string">"r"</span><span>)) </span> </li> <li> <span class="func">echo</span><span> </span><span class="string">"打开文件{$filename2}不成功"</span><span>; </span> </li> </ol>
输出结果:打开文件userinfo.txt成功
2.使用完毕文件后,要显式的告诉PHP已经使用完文件,让操作系统确保将文件的所有内容正确地从缓冲区刷新到硬盘
使用fclose()关闭文件,
<ol class="dp-c"><li class="alt"><span><span>bool fclose ( resource handle )</span><span class="comment">//关闭一个已打开的文件指针 </span><span> </span></span></li></ol>
3.读取文件,fopen函数的mode参数允许读取,PHP提供了几个函数从文件读取数据
string fgets ( int handle [, int length] )从文件指针中读取一行,在二进制文件上尝试fgets会产生不可预测的结果
如果不指定长度,默认读取1K数据,碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止
string fgetss ( resource handle [, int length [, string allowable_tags]] )从文件指针中读取一行并过滤掉 HTML 标记
fgetc()读取单个字符
fread()读取任意二进制数据
代码部分:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span> (</span><span class="string">"test.jpg"</span><span>, </span><span class="string">"rb"</span><span>); </span></span></li> <li> <span class="vars">$c</span><span>; </span> </li> <li class="alt"> <span class="keyword">while</span><span> (!</span><span class="func">feof</span><span>(</span><span class="vars">$handle</span><span>)) { </span> </li> <li> <span class="vars">$contents</span><span> .= @</span><span class="func">fread</span><span>(</span><span class="vars">$handle</span><span>, 8192);</span><span class="comment">//循环读取并将其合并为一个大块文件 </span><span> </span> </li> <li class="alt"><span>} </span></li> <li> <span>fclose(</span><span class="vars">$handle</span><span>); </span> </li> </ol>
4.判断文件读取的状态
每个文件句柄都有一个文件指针,或者一个指出下一个操作将在文件中哪里发生的游标,根据fopen函数的mode参数
文件指针最初位于文件的开头(0),或者文件的末尾
feof()可以判断文件是否已经到末尾(到末尾后函数返回TRUE)
filesize()函数返回文件的大小 5.写入文件
fwrite()函数执行文件写入
代码部分:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$filename</span><span> = </span><span class="string">'test.txt'</span><span>; </span></span></li> <li> <span class="vars">$somec</span><span>; </span> </li> <li class="alt"> <span class="comment">// 首先我们要确定文件存在并且可写。 </span><span> </span> </li> <li> <span class="keyword">if</span><span> (</span><span class="func">is_writable</span><span>(</span><span class="vars">$filename</span><span>)) { </span> </li> <li class="alt"> <span class="comment">// 在这个例子里,我们将使用添加模式打开$filename, </span><span> </span> </li> <li> <span class="comment">// 因此,文件指针将会在文件的开头, </span><span> </span> </li> <li class="alt"> <span class="comment">// 那就是当我们使用fwrite()的时候,$somecontent将要写入的地方。 </span><span> </span> </li> <li> <span class="keyword">if</span><span> (!</span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$filename</span><span>, </span><span class="string">'a'</span><span>)) { </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"不能打开文件 $filename"</span><span>; </span> </li> <li> <span class="func">exit</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="comment">// 将$somecontent写入到我们打开的文件中。 </span><span> </span> </li> <li class="alt"> <span class="keyword">if</span><span> (fwrite(</span><span class="vars">$handle</span><span>, </span><span class="vars">$somecontent</span><span>) === FALSE) { </span> </li> <li> <span class="func">echo</span><span> </span><span class="string">"不能写入到文件 $filename"</span><span>; </span> </li> <li class="alt"> <span class="func">exit</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"成功地将 $somecontent 写入到文件$filename"</span><span>; </span> </li> <li> <span>fclose(</span><span class="vars">$handle</span><span>); </span> </li> <li class="alt"> <span>} </span><span class="keyword">else</span><span> { </span> </li> <li><span>} </span></li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"文件 $filename 不可写"</span><span>; </span> </li> </ol>
输出结果:
成功地将 添加这些文字到文件 写入到文件test.txt
对于二进制数据,必须指定第三个参数,它包含写入到磁盘的数据字节数
<ol class="dp-c"><li class="alt"><span><span class="vars">$result</span><span> = @fwrite(</span><span class="vars">$fp</span><span>,</span><span class="vars">$binary_data</span><span>,mb_strlen(</span><span class="vars">$binary_data</span><span>,</span><span class="string">'8bit'</span><span>)); </span></span></li></ol>
6.文件权限和其他信息
7.删除和重命名文件
8.访问目录
目录访问建议使用前向斜线"/",兼容windows和unix系统
希望以上内容介绍的是八中常见的文件操作方式,对你有帮助。