Home >php教程 >php手册 >PHP文件系统相关函数的功能详解

PHP文件系统相关函数的功能详解

WBOY
WBOYOriginal
2016-06-13 11:07:041227browse

我们通过对关于文件路径的解释:在Windows中,可以在路径中使用/和符号,而在其他的操作系统中只能使用/符号。出于一致性的缘故,我们统一使用/符号。

下面的PHP文件系统相关函数脚本样例可以显示一个目录清单,注释已经包含在代码中:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> /*把要读取的目录的全路径名存入一个名字为$dir_name的变量中。 */   </span></span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">dir_name</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"/home/me/"</font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 创建一个句柄,其值是打开一个给定目录的结果*/   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">dir</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">opendir</font></span><span>($dir_name);   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 建立一个文字块,用以放置列表元素(文件名字)*/   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">file_list</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"<ul>"</ul></font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 使用一个while语句,读取已经打开的目录中的所有元素,如果文件的名字不是“.”或“..”,则显示列表中的名字*/   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>while ($</span><span class="attribute"><font color="#ff0000">file_name</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">readdir</font></span><span>($dir)) {   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>if (($file_name != ".") && ($file_name != "..")) {   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$file_list </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>$file_name"</li></font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$file_list </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">""</font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/*关闭打开的目录,结束PHP模块*/   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>closedir($dir);   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="comments"><font color="#008200"><!-- Start your HTML --></font></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">HTML</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">HEAD</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">TITLE</span><span class="tag">></span></span></font></strong><span>Directory Listing</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">TITLE</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">HEAD</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">BODY</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="comments"><font color="#008200"><!-- Use PHP to print the name of the directory you read --></font></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">P</span><span class="tag">></span></span></font></strong><span>Files in: </span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span><span class="tag-name"><strong><font color="#006699">echo</font></strong></span><span> "$dir_name"; </span><strong><font color="#006699"><span class="tag">?></span><span class="tag"></span><span class="tag-name">p</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="comments"><font color="#008200"><!-- Use PHP to print the directory listing --></font></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span><span class="tag-name"><strong><font color="#006699">echo</font></strong></span><span> "$file_list"; </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">BODY</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">HTML</span><span class="tag">></span></font></strong><span>  </span>
</li>
</ol>

好了,我们已经得到了一个目录清单。需要注意的是,要读取一个文件(稍后我们将进行讲解)或目录的内容,PHP运行的系统上的用户必须至少有读取文件的权限。

下面是一个如何拷贝文件的PHP文件系统相关函数例子:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> /*把你要拷贝的原文件的全路径赋给一个名字为$original的变量中,把拷贝的文件的全路径赋予一个名字为$copied的变量中*/   </span></span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">original</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"/home/me/mydatabasedump"</font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">copied</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"/archive/mydatabasedumo_1010"</font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 使用copy()函数拷贝原始文件,如果拷贝没有完成则会显示一个错误信息*/   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>@copy($original, $copied) or die("Couldn't copy file.");   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>  </span>
</li>
</ol>

这个PHP文件系统相关函数的例子是一个文件备份系统的原型。在这段脚本运行时,它把文件拷贝到一个不同的位置进行保存。稍微修改一下守护程序,就可以在一天中你指定的时刻执行它,而无需用户的干预。

假定你在系统上安装了Lynx,可以创建一个守护程序的条目访问这个文件,访问这个文件会运行这个脚本并建立一个拷贝文件,下面的例子将在上午5点钟运行这个脚本,然后关闭Lynx:

0 5 * * * [username] lynx -dump http://localhost/copyfile.php 1>/dev/null 2>&1

如果运行的是CGI版本的PHP,可以跳过Lynx部分,而直接调用二进制文件:

0 5 * * * [username] php /path/to/copyfile.php 1>/dev/null 2>&1


Statement:
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