Open the directory in php and output directory file to achieve the code

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags close closedir code directory echo file function name

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.