Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 269575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:51:33+00:00 2026-05-11T23:51:33+00:00

I tried to write a script to list all files in directories and subdirectories

  • 0

I tried to write a script to list all files in directories and subdirectories and so on.. The script works fine if I don’t include the check to see whether any of the files are directories. The code doesn’t generate errors but it generates a hundred lines of text saying “Directory Listing of .” instead of what I was expecting. Any idea why this isn’t working?

<?php

//define the path as relative
$path = "./";

function listagain($pth)
{
//using the opendir function
$dir_handle = @opendir($pth) or die("Unable to open $pth");

echo "Directory Listing of $pth<br/>";

//running the while loop
while ($file = readdir($dir_handle)) 
{
    //check whether file is directory
    if(is_dir($file))
    {
        //if it is, generate it's list of files
        listagain($file);
    }
    else
    {
        if($file!="." && $file!="..")
        echo "<a href='$file'>$file</a><br/>";
    }
}
//closing the directory
closedir($dir_handle);
}

listagain($path)

?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:51:34+00:00Added an answer on May 11, 2026 at 11:51 pm

    The first enties . and .. refer to the current and parent directory respectivly. So you get a infinite recursion.

    You should first check for that before checking the file type:

    if ($file!="." && $file!="..") {
        if (is_dir($file)) {
            listagain($file);
        } else {
            echo '<a href="'.htmlspecialchars($file).'">'.htmlspecialchars($file).'</a><br/>';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a script that finds all executable files in a directory.
I tried to do <script type=text/javascript> //<![CDATA[ document.write('<meta http-equiv=refresh content=0;url=index.php />'); //]]> </script> but
I tried to write a function to check key code with JavaScript. It's working
I've tried to write a string replace function in C, which works on a
I would like to log all the output of a Python script. I tried:
I am trying to create a script that would list all the linked libraries
I tried to write a function that calculates a hamming distance between two codewords
I tried to write a Neural Network system, but even running through simple AND/OR/NOR
I tried to write a program that uses threads, but couldn't understand the o/p.
I tried to write a Taylor series expansion for exp(x)/sin(x) using fortran, but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.