Home > Backend Development > PHP Tutorial > php开发之文件目录处置

php开发之文件目录处置

WBOY
Release: 2016-06-13 12:17:34
Original
987 people have browsed it

php开发之文件目录处理

1,打开/关闭文件目录
打开/关闭文件目录和打开/关闭文件类似,但是打开的文件如果不存在,就自动创建一个文件。而打开文件的路径如果不正确,那么就会报错。
(1) 打开目录
PHP中使用opendir()函数来打开目录,语法如下:
resource opendir(string path)
path 为一个合法的目录路径,成功执行后返回一个目录指针。如果path不是一个合法的目录路径或者因为权限或系统错误而无法打开则返回false。并产生一个错误警告。可以在opendir()[email protected]

(2) 关闭目录
关闭目录使用closedir()函数,语法如下:
void closedir(resource handle)
参数handle 为opendir()函数返回的一个目录指针。

打开/关闭目录的示例代码如下:

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span><span class="hljs-variable">$filepath</span> =<span class="hljs-string">'/Applications/XAMPP/xamppfiles/htdocs/PHPTEST/'</span>;<span class="hljs-keyword">if</span> (is_dir(<span class="hljs-variable">$filepath</span>)) {                <span class="hljs-comment">//检测是否是一个目录</span>    <span class="hljs-keyword">if</span> (<span class="hljs-variable">$dir</span> =opendir(<span class="hljs-variable">$filepath</span>)) {        <span class="hljs-keyword">echo</span> <span class="hljs-string">"目录如下:"</span>;        <span class="hljs-keyword">echo</span> <span class="hljs-variable">$dir</span>;    }    <span class="hljs-keyword">else</span> {        <span class="hljs-keyword">echo</span> <span class="hljs-string">"<<<<目录错误"</span>;        <span class="hljs-keyword">exit</span>();    }    closedir(<span class="hljs-variable">$dir</span>);}<span class="hljs-keyword">else</span> {    <span class="hljs-keyword">echo</span> <span class="hljs-string">"目录错误>>>"</span>;}<span class="hljs-preprocessor">?></span></span></code>
Copy after login

运行结果如下:
这里写图片描述

(3)浏览目录
在php中浏览目录使用的是 scandir()函数,语法如下:
array scandir(string diretory[,int sorting_order])
该函数返回了目录下的所有文件和目录,参数sorting_order 指定排序顺序,默认按字母生序排列。如果添加了参数,按照降序排列。

示例代码如下:

<code class=" hljs php"><span class="hljs-preprocessor"><?php</span><span class="hljs-variable">$filepath</span> =<span class="hljs-string">'/Applications/XAMPP/xamppfiles/htdocs/PHPTEST/'</span>;<span class="hljs-keyword">if</span> (is_dir(<span class="hljs-variable">$filepath</span>)) {                <span class="hljs-comment">//检测是否是一个目录</span>    <span class="hljs-variable">$dir</span> =scandir(<span class="hljs-variable">$filepath</span>);    <span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$dir</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$value</span>){        <span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>.<span class="hljs-string">'<br>'</span>;    }}<span class="hljs-keyword">else</span> {    <span class="hljs-keyword">echo</span> <span class="hljs-string">"目录错误>>>"</span>;}<span class="hljs-preprocessor">?></span></code>
Copy after login

运行结果如下:
这里写图片描述

常见的目录操作函数如下表:
这里写图片描述

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