Usage precautions when php is_file() and is_dir() are used to traverse directories_PHP Tutorial

WBOY
Release: 2016-07-21 15:40:07
Original
1368 people have browsed it

1. The directory inc has the following contents:
Subdirectory 0
Subdirectory a
footer.html
header.html
login_function.inc.php
mysqli_connect .php
style.css

2. Now PHP needs to traverse the inc directory and only display files, not directories 0 and a. The code is as follows:

Copy code The code is 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 result only shows that "footer.html" is a file, and the others have become directories:
is a directory.
is a directory ..
is the 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 "$f" cannot be used directly in is_file and is_dir. This will be regarded as the file in the root directory by PHP. However, there is the file footer.html in my root directory, so it will Display this file correctly. Others don't. Change the code to:
To display correctly, you need to modify the code:
Copy the code The code is as follows:

while(false !== ($f=readdir($d)))
{
if(is_file("$dir/$f")){
echo "

$f

";
}else{
echo "

is the directory $f

";
}
}
closedir($d);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321411.htmlTechArticle1. The directory inc has the following contents: Subdirectory 0 Subdirectory a footer.html header.html login_function.inc. php mysqli_connect.php style.css 2. Now PHP needs to traverse the inc directory and only display files...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!