The opendir () function opens a directory handle that can be used by closedir (), readdir (), and rewinddir ().
grammar
opendir (path, context)
* /
$ d = dir ("c: windows"); // Open the directory and assign it to the variable
echo "handle:". $ d-> handle. "n"; // Output the handel attribute of the directory
echo "<p>";
echo "path:". $ d-> path. "n"; // Output the path attribute of the directory
echo "<p>";
while (false! == ($ entry = $ d-> read ())) // method of reading the object by loop
{
echo $ entry. "n";
}
$ d-> close (); / / close the directory handle
//
$ dir = "c: windows"; / / define the directory
if (is_dir ($ dir)) // if the variable is a directory
{
if ($ dh = opendir ($ dir)); // if the directory is opened successfully
{
echo "directory." $ dir. "successfully opened";
}
}
echo "<br>";
closedir ($ dh); // Close the open directory handle
echo "directory." $ dir. "is closed!";
///
$ dir = "c: windows"; / / define directory variables
if (is_dir ($ dir)) / / determine whether the variable is a directory
{
if ($ dh = opendir ($ dir)) / / to determine whether the directory opens successfully
{
while (($ file = readdir ($ dh))! == false) // loop to read the file information in the directory
{
echo "filename: $ file, filetype:". filetype ($ dir. $ file). "n"; / / output file name and file type
}
closedir ($ dh); // Close the directory handle
}
}
///
if ($ handle = opendir ('web')) // If the directory is opened smoothly
{
while (false! == ($ file = readdir ($ handle))) // Loop through the entries in the directory
{
if ($ file! = "." && $ file! = "..") // If the file is not current or parent directory
{
echo "$ filen"; / / output file name
}
}
closedir ($ handle); // Close the directory handle
}
//
if ($ handle = opendir ('08 ')) // If the directory is opened smoothly
{
while (false! == ($ file = readdir ($ handle))) // Loop through the entries in the directory
{
if ($ file! = "." && $ file! = "..") // If the file is not current or parent directory
{
echo "$ filen"; / / output file name
}
}
rewind ($ handle); // rewind directory handle
if (readdir ($ handle)) / / determine whether the success of the rewind
{
echo "successful return";
}
else
{
echo "fail to rewind";
}
closedir ($ handle); // Close the directory handle
}
// If you want to sort the directory alphabetically as one
$ files1 = scandir ($ dir); // List the contents into the array in ascending alphabetical order
$ files2 = scandir ($ dir, 1); // Sort the contents into the array in descending alphabetical order