Jump to content


Photo

suggestions needed


  • Please log in to reply
3 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 September 2006 - 03:44 AM

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

<?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);
?>

I tried doing another while loop in the
if (is_dir($file) !== false) {
but that timed out.

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

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 marker5a

marker5a
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 04 September 2006 - 03:46 AM

Looks about as simple as it can get... cant you just change the timeout time in the ini file?

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 September 2006 - 04:01 AM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 10:31 AM

recursion is what you need, and a lot of patience.

<?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));

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users