opendir, readdir and closedir (PHP operating directories and files) This is a simple introductory tutorial for PHP directory and file operations. We mainly talk about the use of the three functions opendir, readdir and closedir.
opendir, readdir and closedir (php tutorial operating directories and files)
This is a simple introductory tutorial for PHP directory and file operations. We mainly talk about the use of three functions: opendir, readdir and closedir.
*/
$dir = "d:www.bkjia.com";
//Open the directory $dir and assign the directory handle to the variable $dh
if($dh = opendir($dir))
{
//Through the while loop, use the function readdir to get the file name
While(($file_name = readdir($dh)) !== false)
{
echo "file name: ".$file_name;
echo "
";
echo "
";
}
//After processing is completed, close the directory handle $dh
closedir($dh);
}
/*
opendir definition and usage
The opendir() function opens a directory handle and can be used by closedir(), readdir() and rewinddir().
If successful, this function returns a directory stream, otherwise it returns false and an error. You can hide error output by prepending "@" to the function name.
Grammar
opendir(path,context)
readdir definition and usage
The readdir() function returns the entry in the directory handle opened by opendir().
If successful, the function returns a file name, otherwise it returns false.
Grammar
readdir(dir_stream)
The closedir() function closes the directory handle opened by the opendir() function.
Grammar
closedir(dir_stream)