Jump to content

Archived

This topic is now archived and is closed to further replies.

tomfmason

suggestions needed

Recommended Posts

I am attempting to look through a directory and record all sub directories/files into two arrays. Here is what I have so far.

[code]
<?php
clearstatcache();
$folders = array();
$files = array();
$path = "./";

if ($dir = opendir($path)) {
   $d = 0;
   $f = 0;
   while (false !== ($file = readdir($dir))) {
        if ($file != '.' && $file != '..') [
           if (is_dir($file) !== false) {
              $folders[$d] = $file;
              $d++;
           //I want to loop through these folders and add any sub folders as well as the files
           //to the files array
          }
          if (is_file($file) !== false) {
             $files[$f] = "main, $file";
             $f++;
         }
      }
   }
}
closedir($dir);
?>
[/code]

I tried doing another while loop in the [code=php:0]if (is_dir($file) !== false) {[/code] but that timed out.

Any suggestions as to a better way to do this would be great.

Thanks,
Tom

Share this post


Link to post
Share on other sites
I guess I could but I already have the time out set to 60 seconds. I don't realy want the script to take more time then that.

Any other suggestions?

Thanks again,
Tom

Share this post


Link to post
Share on other sites
recursion is what you need, and a lot of patience.

[code]<?php

function filelist($dir)
{
    if (!$dir = realpath($dir)) return null;

    static $files = array();
    static $dirs = array();

    $handle = opendir($dir);

    while (($file = readdir($handle)) !== false)
    {
        if (!in_array($file, array('.', '..')))
        {
            if (is_dir($path = ($dir . DIRECTORY_SEPARATOR . $file)))
            {
                $dirs[] = $path;
                filelist($path);
            } else {
                $files[] = $path;
            }
        }
    }
   
    return array('dirs' => $dirs, 'files' => $files);
}

echo nl2br(print_r(filelist('.'), true));

?>[/code]

Share this post


Link to post
Share on other sites

×

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.