Heim > Backend-Entwicklung > PHP-Tutorial > 详细介绍PHP的目录操作和文件操作_PHP教程

详细介绍PHP的目录操作和文件操作_PHP教程

WBOY
Freigeben: 2016-07-20 10:57:50
Original
902 Leute haben es durchsucht

关于PHP中的目录操作和文件操作,也曾介绍过,不过有的人还是不是很明白。下面详细介绍PHP的目录操作和文件操作。

在任何计算机设备中,文件是都是必须的对象,而在Web编程中,文件的操作一直是web程序员的头疼的地方,而,文件的操作在cms系统中这是必须的,非常有用的,我们经常遇到生成文件目录,文件(夹)编辑等操作,现在我把PHP中的这些函数做一详细总结并实例示范如何使用.,关于对应的函数详细介绍,请查阅php手册.此处只总结重点.和需要注意的地方.(这在php手册是没有的.)

一、目录操作

首先介绍的是一个从目录读取的函数,opendir(),readdir(),closedir(),使用的时候是先打开文件句柄,而后迭代列出:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$base_dir</span><span> = </span><span class="string">"filelist/"</span><span>;   </span>
</li>
<li class="alt">
<span class="vars">$fso</span><span> = opendir(</span><span class="vars">$base_dir</span><span>);   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$base_dir</span><span>."  </span>
</li>
<li class="alt"><span> </span></li>
<li><span>" ;   </span></li>
<li class="alt">
<span class="keyword">while</span><span>(</span><span class="vars">$flist</span><span>=readdir(</span><span class="vars">$fso</span><span>)){   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$flist</span><span>."   </span>
</li>
<li class="alt"><span>" ;   </span></li>
<li><span>}   </span></li>
<li class="alt">
<span class="func">closedir</span><span>(</span><span class="vars">$fso</span><span>)   </span>
</li>
<li><span>?> </span></li>
</ol>
Nach dem Login kopieren

这是讲返回文件目录下面的文件已经目录的程序(0文件将返回false).

有时候需要知道目录的信息,可以使用dirname($path)和basename($path),分别返回路径的目录部分和文件名名称部分,可用disk_free_space($path)返回看空间空余空间.

创建命令:

mkdir($path,0777),0777是权限码,在非window下可用umask()函数设置rmdir($path)

将删除路径在$path的文件.

dir -- directory 类也是操作文件目录的重要类,有3个方法,read,rewind,close,这是一个仿面向对象的类,它先使用的是打开文件句柄,然后用指针的方式读取的.,这里看php手册:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$d</span><span> = dir(</span><span class="string">"/etc/php5"</span><span>);   </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">"Handle: "</span><span> . </span><span class="vars">$d</span><span>->handle . </span><span class="string">"n"</span><span>;   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="string">"Path: "</span><span> . </span><span class="vars">$d</span><span>->path . </span><span class="string">"n"</span><span>;   </span>
</li>
<li class="alt">
<span class="keyword">while</span><span> (false !== (</span><span class="vars">$entry</span><span> = </span><span class="vars">$d</span><span>->read())) {   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$entry</span><span>.</span><span class="string">"n"</span><span>;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="vars">$d</span><span>->close();   </span>
</li>
<li class="alt"><span>?> </span></li>
</ol> 
Nach dem Login kopieren

输出:

<ol class="dp-xml">
<li class="alt"><span><span>Handle: Resource id #2   </span></span></li>
<li><span>Path: /etc/php5   </span></li>
<li class="alt"><span>.   </span></li>
<li><span>..   </span></li>
<li class="alt"><span>apache   </span></li>
<li><span>CGI   </span></li>
<li class="alt"><span>cli </span></li>
</ol>
Nach dem Login kopieren

文件的属性也非常重要,文件属性包括创建时间,最后修改时间,所有者,文件组,类型,大小等.

下面我们重点谈文件操作.

二、文件操作

A、读文件

首先是一个文件看能不能读取(权限问题),或者存在不,我们可以用is_readable函数获取信息。

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span>
</li>
<li class="alt">
<span class="keyword">if</span><span> (</span><span class="func">is_readable</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span>
</li>
<li>
<span class="keyword">die</span><span>(</span><span class="string">'文件不存在或者无法读取'</span><span>);   </span>
</li>
<li class="alt">
<span>} </span><span class="keyword">else</span><span> {   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="string">'存在'</span><span>;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>?> </span></li>
</ol>
Nach dem Login kopieren

判断文件存在的函数还有file_exists(下面演示),但是这个显然无is_readable全面.,当一个文件存在的话可以用

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">"filelist.php"</span><span>;   </span>
</li>
<li class="alt">
<span class="keyword">if</span><span> (</span><span class="func">file_exists</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span>
</li>
<li>
<span class="keyword">die</span><span>(</span><span class="string">'文件不存在'</span><span>);   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="vars">$data</span><span> = </span><span class="func">file_get_contents</span><span>(</span><span class="vars">$file</span><span>);   </span>
</li>
<li class="alt">
<span class="func">echo</span><span> htmlentities(</span><span class="vars">$data</span><span>);   </span>
</li>
<li><span>?> </span></li>
</ol>
Nach dem Login kopieren

但是file_get_contents函数在较低版本上不支持,可以先创建文件的一个句柄,然后用指针读取全部:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$fso</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$cacheFile</span><span>, </span><span class="string">'r'</span><span>);   </span></span></li>
<li>
<span class="vars">$data</span><span> = </span><span class="func">fread</span><span>(</span><span class="vars">$fso</span><span>, </span><span class="func">filesize</span><span>(</span><span class="vars">$cacheFile</span><span>));   </span>
</li>
<li class="alt">
<span>fclose(</span><span class="vars">$fso</span><span>); </span>
</li>
</ol>
Nach dem Login kopieren

还有一种方式,可以读取二进制的文件:

<ol class="dp-c"><li class="alt"><span><span class="vars">$data</span><span> = implode(</span><span class="string">''</span><span>, file(</span><span class="vars">$file</span><span>)); </span></span></li></ol>
Nach dem Login kopieren

B、写文件

和读取文件的方式一样,先看看是不是能写:

<ol class="dp-c">
<li class="alt"><span><span> <?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span>
</li>
<li class="alt">
<span class="keyword">if</span><span> (</span><span class="func">is_writable</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span>
</li>
<li>
<span class="keyword">die</span><span>(</span><span class="string">"我是鸡毛,我不能"</span><span>);   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>?> </span></li>
</ol>
Nach dem Login kopieren

能写了的话可以使用file_put_contents函数写入

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span>
</li>
<li class="alt">
<span class="keyword">if</span><span> (</span><span class="func">is_writable</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span>
</li>
<li>
<span class="keyword">die</span><span>(</span><span class="string">'我是鸡毛,我不能'</span><span>);   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="vars">$data</span><span> = </span><span class="string">'我是可鄙,我想要'</span><span>;   </span>
</li>
<li class="alt">
<span class="func">file_put_contents</span><span> (</span><span class="vars">$file</span><span>, </span><span class="vars">$data</span><span>);   </span>
</li>
<li><span>?> </span></li>
</ol>
Nach dem Login kopieren

file_put_contents函数在php5中新引进的函数(不知道存在的话用function_exists函数先判断一下)低版本的php无法使用,可以使用如下方式:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$f</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$file</span><span>, </span><span class="string">'w'</span><span>);   </span></span></li>
<li>
<span>fwrite(</span><span class="vars">$f</span><span>, </span><span class="vars">$data</span><span>);   </span>
</li>
<li class="alt">
<span>fclose(</span><span class="vars">$f</span><span>); </span>
</li>
</ol>
Nach dem Login kopieren

替换之.

写文件的时候有时候需要锁定,然后写:

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> cache_page(</span><span class="vars">$pageurl</span><span>,</span><span class="vars">$pagedata</span><span>){   </span></span></li>
<li>
<span class="keyword">if</span><span>(!</span><span class="vars">$fso</span><span>=</span><span class="func">fopen</span><span>(</span><span class="vars">$pageurl</span><span>,</span><span class="string">'w'</span><span>)){   </span>
</li>
<li class="alt">
<span class="vars">$this</span><span>->warns(</span><span class="string">'无法打开缓存文件.'</span><span>);</span><span class="comment">//trigger_error  </span><span> </span>
</li>
<li>
<span class="keyword">return</span><span> false;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="keyword">if</span><span>(!</span><span class="func">flock</span><span>(</span><span class="vars">$fso</span><span>,LOCK_EX)){</span><span class="comment">//LOCK_NB,排它型锁定  </span><span> </span>
</li>
<li class="alt">
<span class="vars">$this</span><span>->warns(</span><span class="string">'无法锁定缓存文件.'</span><span>);</span><span class="comment">//trigger_error  </span><span> </span>
</li>
<li>
<span class="keyword">return</span><span> false;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="keyword">if</span><span>(!fwrite(</span><span class="vars">$fso</span><span>,</span><span class="vars">$pagedata</span><span>)){</span><span class="comment">//写入字节流,serialize写入其他格式  </span><span> </span>
</li>
<li class="alt">
<span class="vars">$this</span><span>->warns(</span><span class="string">'无法写入缓存文件.'</span><span>);</span><span class="comment">//trigger_error  </span><span> </span>
</li>
<li>
<span class="keyword">return</span><span> false;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="func">flock</span><span>(</span><span class="vars">$fso</span><span>,LOCK_UN);</span><span class="comment">//释放锁定  </span><span> </span>
</li>
<li class="alt">
<span>fclose(</span><span class="vars">$fso</span><span>);   </span>
</li>
<li>
<span class="keyword">return</span><span> true;   </span>
</li>
<li class="alt"><span>} </span></li>
</ol>
Nach dem Login kopieren

C、复制,删除文件

php删除文件非常easy,用unlink函数简单操作:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span>
</li>
<li class="alt">
<span class="vars">$result</span><span> = @unlink (</span><span class="vars">$file</span><span>);   </span>
</li>
<li>
<span class="keyword">if</span><span> (</span><span class="vars">$result</span><span> == false) {   </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">'蚊子赶走了'</span><span>;   </span>
</li>
<li>
<span>} </span><span class="keyword">else</span><span> {   </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">'无法赶走'</span><span>;   </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>?> </span></li>
</ol>
Nach dem Login kopieren

即可.

复制文件也很容易:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'yang.txt'</span><span>;   </span>
</li>
<li class="alt">
<span class="vars">$newfile</span><span> = </span><span class="string">'ji.txt'</span><span>; # 这个文件父文件夹必须能写   </span>
</li>
<li>
<span class="keyword">if</span><span> (</span><span class="func">file_exists</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span>
</li>
<li class="alt">
<span class="keyword">die</span><span> (</span><span class="string">'小样没上线,无法复制'</span><span>);   </span>
</li>
<li><span>}   </span></li>
<li class="alt">
<span class="vars">$result</span><span> = </span><span class="func">copy</span><span>(</span><span class="vars">$file</span><span>, </span><span class="vars">$newfile</span><span>);   </span>
</li>
<li>
<span class="keyword">if</span><span> (</span><span class="vars">$result</span><span> == false) {   </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">'复制记忆ok'</span><span>;   </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>?> </span></li>
</ol>
Nach dem Login kopieren

可以使用rename()函数重命名一个文件夹.其他操作都是这几个函数组合一下就能实现的.

D、获取文件属性

我说几个常见的函数:

获取最近修改时间:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'test.txt'</span><span>;   </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">'r'</span><span>, </span><span class="func">filemtime</span><span>(</span><span class="vars">$file</span><span>));   </span>
</li>
<li><span>?> </span></li>
</ol>
Nach dem Login kopieren

返回的说Unix的时间戳,这在缓存技术常用.

相关的还有获取上次被访问的时间fileatime(),filectime()当文件的权限,所有者,所有组或其它 inode 中的元数据被更新时间,fileowner()函数返回文件所有者

<ol class="dp-c"><li class="alt"><span><span class="vars">$owner</span><span> = posix_getpwuid(</span><span class="func">fileowner</span><span>(</span><span class="vars">$file</span><span>)); </span></span></li></ol>
Nach dem Login kopieren

(非window系统),ileperms()获取文件的权限,

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span>
</li>
<li class="alt">
<span class="vars">$perms</span><span> = </span><span class="func">substr</span><span>(sprintf(</span><span class="string">'%o'</span><span>, </span><span class="func">fileperms</span><span>(</span><span class="vars">$file</span><span>)), -4);   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$perms</span><span>;   </span>
</li>
<li class="alt"><span>?> </span></li>
</ol>
Nach dem Login kopieren

filesize()返回文件大小的字节数:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="comment">// 输出类似:somefile.txt: 1024 bytes  </span><span> </span>
</li>
<li class="alt">
<span class="vars">$filename</span><span> = </span><span class="string">'somefile.txt'</span><span>;   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$filename</span><span> . </span><span class="string">': '</span><span> . </span><span class="func">filesize</span><span>(</span><span class="vars">$filename</span><span>) . </span><span class="string">' bytes'</span><span>;   </span>
</li>
<li class="alt"><span>?> </span></li>
</ol>
Nach dem Login kopieren

获取文件的全部信息有个返回数组的函数stat()函数:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span>
</li>
<li class="alt">
<span class="vars">$perms</span><span> = stat(</span><span class="vars">$file</span><span>);   </span>
</li>
<li>
<span>var_dump(</span><span class="vars">$perms</span><span>);   </span>
</li>
<li class="alt"><span>?> </span></li>
</ol>
Nach dem Login kopieren

那个键对应什么可以查阅详细资料,此处不再展开.

四、结束语

上面我简要的总结了一下几个文件操作,如果您熟练掌握以上列出的函数,已经在操作的时候没什么大的问题,php文件操作的函数变化比较快,现在已经非常强大了,文件这部分也是学习php非常重要的一部分,希望不要忽略.


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445748.htmlTechArticle关于 PHP 中的目录操作和文件操作,也曾介绍过,不过有的人还是不是很明白。下面详细介绍PHP的目录操作和文件操作。 在任何计算机设备...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage