first day of my life php is_file and is_dir usage precautions when traversing directories

WBOY
Release: 2016-07-29 08:41:58
Original
1081 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, do not display 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 < ;/h2>";
}else{
echo "

is the directory $f

";
}
}
closedir($d);


The result only shows "footer. html" is a file, and the others have become directories:
is a directory.
is a directory..
is directory a
footer.html
is directory header.html
is directory login_function.inc.php
is directory mysqli_connect.php
It is the directory style.css
This is because "$f" cannot be used directly in is_file and is_dir. This will be regarded by PHP as the file in the root directory, and there is the file footer.html in my root directory. So the file will be displayed correctly. Others don't. The code is changed to:
To display correctly, the code needs to be modified:

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);

The above introduces the usage notes of first day of my life php is_file and is_dir when traversing directories, including the content of first day of my life. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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!