Home  >  Article  >  php教程  >  opendir、readdir和closedir(PHP操作目录和文件)

opendir、readdir和closedir(PHP操作目录和文件)

WBOY
WBOYOriginal
2016-06-13 11:17:371004browse

opendir、readdir和closedir(PHP操作目录和文件)这是一款简单的php目录与文件操作的入门教程,我们主要是讲三个函数opendir、readdir和closedir的使用方法。  

opendir、readdir和closedir(php教程操作目录和文件)
这是一款简单的php目录与文件操作的入门教程,我们主要是讲三个函数opendir、readdir和closedir的使用方法。
*/

$dir = "d:www.bkjia.com";

//打开目录$dir,并将目录句柄赋给变量$dh
if($dh = opendir($dir))
{
    //通过while循环,使用函数readdir获取文件名
    while(($file_name = readdir($dh)) !== false)
    {
        echo "file name: ".$file_name;
        echo "
";
        echo "
";
    }
   
    //处理完成后,关闭目录句柄$dh
    closedir($dh);
}

/*
opendir定义和用法
opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用。

若成功,则该函数返回一个目录流,否则返回 false 以及一个 error。可以通过在函数名前加上 "@" 来隐藏 error 的输出。

语法
opendir(path,context)


readdir定义和用法
readdir() 函数返回由 opendir() 打开的目录句柄中的条目。

若成功,则该函数返回一个文件名,否则返回 false。

语法
readdir(dir_stream)

closedir() 函数关闭由 opendir() 函数打开的目录句柄。

语法
closedir(dir_stream)


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