Jump to content

Archived

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

stickman

Retrieve all files in subdirectories

Recommended Posts

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:

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

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Another way to do it would be readdir()

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

Share this post


Link to post
Share on other sites
Recursion..

[code]<?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('.');

?>[/code]

Share this post


Link to post
Share on other sites
[quote author=stickman link=topic=106025.msg423765#msg423765 date=1156820399]
I also cant use any loops or it will take like 10 mintues to execute.[/quote]

But thanks for the help.

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.