Home > Backend Development > PHP Tutorial > Things to note when traversing directories with is_file() and is_dir()

Things to note when traversing directories with is_file() and is_dir()

WBOY
Release: 2016-07-25 08:51:58
Original
914 people have browsed it
  1. $dir = $_SERVER['DOCUMENT_ROOT'];
  2. $dir = "$dir/inc/";
  3. $d = opendir($dir);
  4. while(false !==($f=readdir ($d)))
  5. {
  6. if(is_file($f)){
  7. echo "

    $f

    ";
  8. }else{
  9. echo "

    is the directory $f < ;/h2>";

  10. }
  11. }
  12. closedir($d);
Copy the code

The result only shows that "footer.html" is a file, and the others have become directories: is a directory. It's a directory.. Is directory a footer.html Is the directory header.html Is the directory login_function.inc.php Is the directory mysqli_connect.php It’s 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. However, there is the file footer.html in my root directory, so this will be displayed correctly. document. Others don't. Change the code to: To display correctly, the code needs to be modified:

  1. while(false !== ($f=readdir($d)))
  2. {
  3. if(is_file("$dir/$f")){
  4. echo "

    $f< /h2>";

  5. }else{
  6. echo "

    is the directory $f

    ";
  7. }
  8. }
  9. closedir($d);
Copy code


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