Open the Directory in php and output the directory file implementation code

Source: Internet
Author: User
Tags php and

 

Syntax
Opendir (path, context)
*/
$ D = dir ("c: windows"); // open the directory and assign the value to the variable
Echo "handle:". $ d-> handle. "n"; // handel attribute of the output Directory
Echo "<p> ";
Echo "path:". $ d-> path. "n"; // path attribute of the output Directory
Echo "<p> ";
While (false! ==( $ Entry = $ d-> read () // read the object through a loop
{
Echo $ entry. "n ";
}
$ D-> close (); // close the directory handle


//


$ Dir = "c: windows"; // define a directory
If (is_dir ($ dir) // if the variable is a directory
{
If ($ dh = opendir ($ dir); // if the directory is successfully opened
  {
Echo "directory". $ dir. "opened successfully ";
  }
}
Echo "<br> ";
Closedir ($ dh); // Close The OpenEd directory handle
Echo "directory". $ dir. "disabled! ";


///

$ Dir = "c: windows"; // defines directory variables
If (is_dir ($ dir) // determines whether the variable is a directory.
{
If ($ dh = opendir ($ dir) // You can check whether the directory is successfully opened.
  {
While ($ file = readdir ($ dh ))! = False) // read the file information in the directory cyclically
    {
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 successfully opened
{
While (false! ==( $ File = readdir ($ handle) // read entries in the directory cyclically
  {
If ($ file! = "." & $ File! = "...") // If the file is not the current or upper-level Directory
    {
Echo "$ filen"; // name of the output file
    }
  }
Closedir ($ handle); // Close the directory handle
}

//

If ($ handle = opendir ('08') // if the directory is successfully opened
{
While (false! ==( $ File = readdir ($ handle) // read entries in the directory cyclically
  {
If ($ file! = "." & $ File! = "...") // If the file is not the current or upper-level Directory
    {
Echo "$ filen"; // name of the output file
    }
  }
Rewind ($ handle); // returns the directory handle.
If (readdir ($ handle) // You can check whether the result is returned successfully.
  {
Echo "reverse success ";
  }
Else
  {
Echo "failed to return ";
  }
Closedir ($ handle); // Close the directory handle
}

// If you want to sort directories by letters, for example

$ Files1 = scandir ($ dir); // list content in alphabetical order to an array
$ Files2 = scandir ($ dir, 1); // list content in alphabetical order to an array

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.