PHP custom Traverse directory All Files dir (), Readdir () function _php Tutorial

Source: Internet
Author: User

PHP custom Traverse directory All Files dir (), Readdir () function


Method One: Use Dir () to traverse the directory

Dir () function, returns the Directory class instance when successful

The syntax format for the PHP dir () is:

Dir (directory);//directory is a directory name that needs to display the file name, which can contain path information

PHP dir () Usage example: Lists all filenames under the upload directory:

The code is as follows
$dir = @ Dir ("upload");//open upload directory; "@" to block error messages, because sometimes there is no file in the directory where the files need to be displayed, you may report an error and hide the error with "@"
Enumerate all files in the upload directory
while (($file = $dir->read ())!== false)
{
echo "File name:". $file. "
";
}
$dir->close ();
?>

The output is:

Filename:.

Filename:..

File name: logo.gif

File name: arrow.gif

File name: bg.gif

Example

The code is as follows

function tree ($dir)
{
$mydir = Dir ($dir);
while ($file = $mydir->read ())
{
if ($file! = '. ' && $file! = ' ... ')
{
if (Is_dir ("$dir/$file"))
{
Echo ' Directory name: '. $dir. Directory_separator. " $file. '
';
Tree ("$dir/$file");
}else{
echo ' filename: '. $dir. Directory_separator. $file. '
';
}
}
}
$mydir->close ();
}
Tree ('./phpmyadmin ');

Method two uses Readir () to traverse the directory

Definition and usage

The Readdir () function returns an entry in a directory handle opened by Opendir ().

If successful, the function returns a file name, otherwise it returns false.

Grammar

Readdir (Dir_stream)

Example

The code is as follows

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. " $file. '
';
Listdir ($dir. Directory_separator. $file);
}else{
echo ' filename: '. $dir. Directory_separator. $file. '
';
}
}
}
}
Closedir ($handle);
}else{
echo ' Non-valid catalog! ';
}
}
Listdir ('./phpmyadmin ');

http://www.bkjia.com/PHPjc/834970.html www.bkjia.com true http://www.bkjia.com/PHPjc/834970.html techarticle PHP custom Traverse directory All Files dir (), Readdir () function method one: Use Dir () to traverse the directory Dir () function, the successful return directory class instance PHP dir () syntax format is: Dir (di ...

  • 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.