First day of my life php is_file and Is_dir usage considerations for traversing directories

Source: Internet
Author: User
1. Catalogue Inc has the following contents:
Sub-Catalog 0
Sub-Directory A
Footer.html
Header.html
login_function.inc.php
mysqli_connect.php
Style.css
2, now PHP to traverse the INC directory, and only display files, do not display directories 0 and a, the code is as follows:

Copy the Code code as follows:


$dir = $_server[' document_root ');
$dir = "$dir/inc/";
$d = Opendir ($dir);
while (False!== ($f =readdir ($d)))
{
if (Is_file ($f)) {
echo "

$f

";
}else{
echo "

is the directory $f

";
}
}
Closedir ($d);


The results only show that "footer.html" is a file, and the others become directories:
is the directory.
is the directory:
is directory A
Footer.html
is the directory header.html
is the directory login_function.inc.php
is the directory mysqli_connect.php
is the directory Style.css
This is because the "$f" can not be used directly in Is_file and Is_dir, this will be treated as PHP as the root directory of the file, and in my root directory has footer.html this file, so the file will be displayed correctly. Others are not. Change the code to:
To display correctly, the code needs to be modified:

Copy the Code code as follows:


while (False!== ($f =readdir ($d)))
{
if (Is_file ("$dir/$f")) {
echo "

$f

";
}else{
echo "

is the directory $f

";
}
}
Closedir ($d);

The above describes the usage considerations for the first day of my life php is_file and Is_dir used to traverse the directory, including the first day's my life content, and I want to be helpful to friends who are interested in PHP tutorials.

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