Jump to content

Where's the endless loop here?


Dudemanguy

Recommended Posts

The function here is supposed to catalog the files in a given folder, and all subfolders, and report back the filenames to the screen as links to themselves. Despite my best amateur efforts, I can't get away from the endless loop errors.

 

function explore($dir, $filePath)
{	$blah = opendir($dir);
while (false !== ($file = readdir($blah)))
{if (is_dir($file))
    {
	$filePath = $filePath.$file.'\\';
	closedir($blah);
	 explore($file, $filePath);}
 else
 	{
 	$filePath = $filePath.$file;
 	 echo "<a href='$filePath'>'$file'</a>";
 	 echo "<br />";}
 	 $fLength = strlen($file);
 	 $fpLength = strlen($filePath);
 	 $filePath = substr($filePath, 0, ($fpLength -$fLength));
}
}

Link to comment
Share on other sites

Hmm, I think that readdir returns just the file name, not the full path. Try the below code (completely untested). You should pass in a complete path ending with a /.

 

function explore($dir)
{	$blah = opendir($dir);
while (false !== ($file = readdir($blah)))
{if (is_dir($dir.$file))
    {
	 explore($dir.$file.'/');}
 else
 	{
 	$filePath = $dir.$file;
 	 echo "<a href='$filePath'>'$file'</a>";
 	 echo "<br />";}
}
closedir($blah);
}

Link to comment
Share on other sites

The readdir returns the filename, and which I construct manually into a path with "$filePath = $filePath.$file.'\\';" although this is the area that I get the most errors. With your code, I got a different error, but with a similar source.

 

I usually get this:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 21515 bytes) in C:\Xampp\xampp\www\htdocs\list.php on line 21

 

Line 21 for me is:

$filePath = $filePath.$file.'\\';

Link to comment
Share on other sites

Your problem is that two of the "folders" returned in a directory are '.' and '..'. Which attempt to traverse the directory tree upwards. You need to exclude those fromthe recursive call. Your function is also overly-complicated. You also shouldn't close the filehandle in the loop/. Otherwise once the first subdirectory is run the process will fail ont he next object in the root directory.

 

function explore($dir)
{
    $handle = opendir($dir);
while (false !== ($file = readdir($handle)))
{
        $fullPath = $dir.DIRECTORY_SEPARATOR.$file;
        if (is_dir($file))
    {
            if($file!='.' && $file!='..')
            {
                explore($fullPath);
            }
        }
        else
        {
            echo "<a href=\"$fullPath\">$file</a><br />\n";
        }
    }
    closedir($handle);
}

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.