4 PHP Traversal Functions opendir (), Readdir (), Closedir (), Rewinddir () Usage Instance Summary

Source: Internet
Author: User
Tags phpmyadmin
This article mainly introduces the PHP traversal directory functions Opendir (), Readdir (), Closedir (), Rewinddir () summary, and gives a comprehensive use of these functions as an example of a simple file browser, the need for friends can refer to the following

When you are programming PHP, you need to browse through the files under a directory on the server, usually as a traversal directory. To get the files and subdirectories under a directory, you need to use the Opendir () function, the Readdir () function, the Closedir () function, and the Rewinddir () function.

① function Opendir ()

The function Opendir () is used to open the specified directory, accept the path and directory name of a directory as a parameter, and the function return value is a directory handle (resource type) that can be used by other directory functions. Returns False if the directory does not exist or does not have access rights.

② function Readdir ()

The function Readdir () reads the specified directory, accepts an actionable directory handle that has been opened with the Opendir () function as a parameter, returns a file name for the current directory pointer position, and moves the directory pointer one bit backwards. Returns False when the pointer is at the end of the directory because no file exists.

③ function Closedir ()

The function Closedir () closes the specified directory and accepts an actionable directory handle that has been opened with the Opendir () function as a parameter. The function has no return value and closes the Open directory after running.

④ function Rewinddir ()

The function Reweinddir () returns a directory handle, accepting an actionable directory handle that has been opened with the Opendir () function as a parameter. Resets the directory pointer to the beginning of the directory, back to the beginning of the directory.

Here is an example of how these functions are used. Note that before using this example, make sure that you have the phpMyAdmin folder under the consent directory. The code looks like this:

The code is as follows:

<?php$num = 0; Used to count the number of subdirectories and files $dirname = ' phpMyAdmin '; Save a directory name for convenience in the current directory $dir_handle = Opendir ($dirname); Open Directory with Opendir//the directory and file name to be traversed using tabular format output echo ' <table border= "0" align= "center" width= "All" cellspacing= "0" cellpadding= "0" > '; Echo ' <caption>

The above program first opens a directory pointer and iterates over it. When you traverse a directory, you include the "." and ".." Two special directories, which you can block if you do not need both of these directories. Of course, the display details will vary depending on the contents of the folder. As you can see from the example above, navigating through a folder in PHP is not such a complicated thing. and PHP also provides an object-oriented way for directory traversal, done by using the "Dir" class. In addition, PHP can retrieve the contents specified under the directory as requested by the user, providing the glob () function to retrieve the specified directory. The function eventually returns an array containing the results of the search.

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.