php function to read subdir content

95 Views Asked by At

I would like to ask what I have to add to make this function to show not only the files on top dir but also the files in subdirs..

private function _populateFileList()
{
    $dir_handle = opendir($this->_files_dir);
    if (! $dir_handle) 
    {
        return false;
    }

    while (($file = readdir($dir_handle)) !== false) 
    {
        if (in_array($file, $this->_hidden_files))
        {
            continue;
        }

        if (filetype($this->_files_dir . '/' . $file) == 'file') 
        {
            $this->_file_list[] = $file;
        }
    }
    closedir($dir_handle);

    return true;
}

Thank you in advance!

3

There are 3 best solutions below

0
On

By this you can get all subdir content

customerdel('FolderPath');



 function customerdel($dirname=null){
      if($dirname!=null){
         if (is_dir($dirname))
           $dir_handle = opendir($dirname);
         if (!$dir_handle)
              return false;
         while($file = readdir($dir_handle)) {
               if ($file != "." && $file != "..") {
                if (!is_dir($dirname."/".$file))
                     echo $dirname."/".$file.'<br>';
                else{
                    echo $dirname.'/'.$file.'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                        customerdel($dirname.'/'.$file);
                }
               }
         }
         closedir($dir_handle);
       }    
    }
0
On

You could implement the recursion yourself, or you could use the existing iterator classes to handle the recursion and filesystem traversal for you:

$dirIterator       = new RecursiveDirectoryIterator('.', FilesystemIterator::SKIP_DOTS);
$recursiveIterator = new RecursiveIteratorIterator($dirIterator);
$filterIterator    = new CallbackFilterIterator($recursiveIterator, function ($file) {
    // adjust as needed
    static $badFiles = ['foo', 'bar', 'baz'];
    return !in_array($file, $badFiles);
});

$files = iterator_to_array($filterIterator);

var_dump($files);
0
On

Here is how you can get a recursive array of all files in a directory and its subdirectories.

The returned array is like: array( [fileName] => [filePath] )

EDIT: I've included a small check if there are filenames in the subdirectories with the same name. If so, an underscore and counter is added to the key-name in the returned array: array( [fileName]_[COUNTER] => [filePath] )

private function getFileList($directory) {
    $fileList = array();
    $handle = opendir($directory);
    if ($handle) {
        while ($entry = readdir($handle)) {
            if ($entry !== '.' and $entry !== '..') {
                if (is_dir($directory . $entry)) {
                    $fileList = array_merge($fileList, $this->getFileList($directory . $entry . '/'));
                } else {
                    $i = 0;
                    $_entry = $entry;

                    // Check if filename is allready in use
                    while (array_key_exists($_entry, $fileList)) {
                        $i++;
                        $_entry = $entry . "_$i";
                    }
                    $fileList[$_entry] = $directory . $entry;
                }
            }
        }
        closedir($handle);
    }
    return $fileList;
}