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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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