Jump to content


Photo

Retrieve all files in subdirectories


  • Please log in to reply
4 replies to this topic

#1 stickman

stickman
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 29 August 2006 - 02:59 AM

For this question, I dont think its possible, but it doesnt hurt to ask does it? Anyway, I cant use any FTP function to do this because they only retrieve the first 1000 files and I need it to retrive all of the files which is more than 1000. I also cant use any loops or it will take like 10 mintues to execute. So with these restrictions, is it possible to retrieve an array with all the filenames (in subdirectory/filename.extension format) within subdirectories. This is what I have currently:

<?php
$data = "";
exec("ls */", $data);
print_r($data);
?>

The problems with that is it doesnt include the subdirectory name, for example (subdir1/file.txt and subdir2/file.txt). Thanks in advance for any help that you can offer.

..:: FREE NINTENDO WII OR $250 CASH ::..
..:: Click here for more information ::..


#2 stickman

stickman
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 29 August 2006 - 03:24 AM

Nevermind my question. That returned the subdirectories name as its own array value and then I was able to convert that into a header in which all the filenames below it fall in that subdirectory. Therefore, that code answered my own question, except I had to do some various array things first.

..:: FREE NINTENDO WII OR $250 CASH ::..
..:: Click here for more information ::..


#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 29 August 2006 - 12:27 PM

Another way to do it would be readdir()

Here is the example from php.net
<?php
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       // Strip out .. and . 
       if ($file != "." && $file != "..") {
           echo "$file\n";
       }
   }
   closedir($handle);
}
?> 


#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 29 August 2006 - 01:01 PM

Recursion..

<?php

function print_directories($dir, $return = false)
{
    $dir = realpath($dir);
    $handle = opendir($dir);

    if (!$handle) return false;

    $output = '';

    while (($file = readdir($handle)) !== false) {

        if ($file != '.' && $file != '..') {
            $output .= $dir . DIRECTORY_SEPARATOR . $file . "\n";

            if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
                $output .= print_directories($dir . DIRECTORY_SEPARATOR . $file, true);
            }
        }
    }

    if ($return) return $output;

    echo $output;
}

print_directories('.');

?>


#5 stickman

stickman
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 29 August 2006 - 05:02 PM

I also cant use any loops or it will take like 10 mintues to execute.


But thanks for the help.

..:: FREE NINTENDO WII OR $250 CASH ::..
..:: Click here for more information ::..





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users