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);
http://www.bkjia.com/PHPjc/321411.htmlwww.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...