Jump to content

Retrieve all files in subdirectories


stickman

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.
Link to comment
https://forums.phpfreaks.com/topic/18971-retrieve-all-files-in-subdirectories/
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.
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]
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]

Archived

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

×
×
  • 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.