PHP user-defined traversal of all files in the directory dir (), readdir () function

Source: Internet
Author: User
Tags phpmyadmin

Method 1: Use dir () to traverse the Directory


Dir () function. If the call succeeds, the Directory class instance is returned.

PHP dir () syntax format:
Dir (directory); // directory is the name of the directory to display the file name, which can contain path information
PHP dir () Usage example: list all file names under the upload Directory:

The code is as follows: Copy code
<? Php
$ Dir = @ dir ("upload"); // open the upload directory; "@" can block error messages, because sometimes there is no file in the directory of the file to be displayed, at this time, an error may be reported. Use "@" to hide the error.
// List all objects in the upload Directory
While ($ file = $ dir-> read ())! = False)
{
Echo "file name:". $ file. "<br/> ";
}
$ Dir-> close ();
?>



Output result:

File name :.
File name :..
File name: logo.gif
File name: arrow.gif
File name: bg.gif

Example

The code is as follows: Copy code

Function tree ($ dir)
{
$ Mydir = dir ($ dir );
While ($ file = $ mydir-> read ())
    {
If ($ file! = '.' & $ File! = '..')
        {
If (is_dir ("$ dir/$ file "))
            {
Echo 'directory name: '. $ dir. DIRECTORY_SEPARATOR.' <font color = "red"> '. $ file.' </font> <br/> ';
Tree ("$ dir/$ file ");
} Else {
Echo 'file name: '. $ dir. DIRECTORY_SEPARATOR. $ file.' <br/> ';
            }
        }
    }
$ Mydir-> close ();
}
Tree ('./phpmyadmin ');




Method 2 use readir () to traverse the Directory

Definition and usage

The readdir () function returns the entries in the directory handle opened by opendir.
If the call succeeds, the function returns a file name; otherwise, false.
Syntax

Readdir (dir_stream)

Example

The code is as follows: Copy code

Header ('content-type: text/html; charset = utf-8 ');

Function listDir ($ dir)
{
If (is_dir ($ dir ))
    {
If ($ handle = opendir ($ dir ))
        {
While ($ file = readdir ($ handle ))
            {
If ($ file! = '.' & $ File! = '..')
                {
If (is_dir ($ dir. DIRECTORY_SEPARATOR. $ file ))
                    {
Echo 'directory name: '. $ dir. DIRECTORY_SEPARATOR.' <font color = "red"> '. $ file.' </font> <br/> ';
ListDir ($ dir. DIRECTORY_SEPARATOR. $ file );
} Else {
Echo 'file name: '. $ dir. DIRECTORY_SEPARATOR. $ file.' <br/> ';
                    }
                }
            }
        }
Closedir ($ handle );
} Else {
Echo 'invalid directory! ';
    }
}
ListDir ('./phpmyadmin ');

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.