-6

How to recursively run into a folder and his subdirectories while adding in each directories an xml file with the content list of the current directory ?

I have tried something like that :

$dir = new DirectoryIterator($folder);
foreach ($dir as $fileinfo) {
    $fd=fopen('index.xml','wb');
    if (!$fileinfo->isDot() && !is_dir($fileinfo)) {
        fwrite($fd,'Something');
    }
}

Edit :

the script should do this :

Open dir then create xml file here with the list of files present here (not dirs) Then open next dir, then create xml file here with the list of files present here (not dirs) Etc...

MrSo
  • 641
  • 8
  • 34
  • 1
    I've used `RecursiveDirectoryIterator` and `RecursiveIteratorIterator` in a project that do this. Can you narrow down your question? How would you define "best"? – complex857 Jun 26 '13 at 20:39
  • best for you to try something, and ask when\if you get stuck –  Jun 26 '13 at 20:39
  • I have change The best way by how to ? :) – MrSo Jun 26 '13 at 20:41
  • I think this is what you want: http://stackoverflow.com/questions/6837479/ – Amal Murali Jun 26 '13 at 20:45
  • @AmalMurali Not exactly... this question is about adding some attibutes into an existing xml file. Not to run in a folder recursively link I need – MrSo Jun 26 '13 at 20:57
  • We wouldn't know unless you tell us. So, edit your question and describe what you're trying to do and show what you've tried so far and what's the expected output. – Amal Murali Jun 26 '13 at 21:00
  • @complex857 I have tried with your solution but I don't know how to list files with this solution – MrSo Jun 26 '13 at 21:43
  • Well the best solution I found is [this one](http://stackoverflow.com/questions/13693480) and tried [this one](http://stackoverflow.com/a/3556894/1458189) but I still don't reach my goal. – MrSo Jun 26 '13 at 21:51
  • 2
    This is not a programming question. You are asking that others write the whole program (not word by word, but see your question, you're asking five things at once or so). So better start with *one* sub-problem and solve it. For each of the sub-problems I found from quickly scanning your question I know answers exist: 1.) Recursive Directory Traversal 2.) Flat Directory Traversal (for the listing to create the XML) 3.) Creating an XML based on data provided from an Interator (like a database result, an array or the said flat directory listing). Solved. – hakre Jun 26 '13 at 23:56
  • If I can't explain clearly my needs I'm sorry. Anyway I couldn't write the piece of code suggested by Complex758 before ! That's why I asked help to this community ! When I can help here, I help back. That's the way it works ? no ? Anyways Thx again – MrSo Jun 27 '13 at 14:35

1 Answers1

1

If you want a flat iteration of a directory structure, try combining the RecursiveDirectoryIterator drived by RecursiveIteratorIterator. This is how the skeleton of the iteration could look like:

$start_dir =  '/some/path';
$rit = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator(
        $start_dir,
        FilesystemIterator::KEY_AS_PATHNAME |
        FilesystemIterator::CURRENT_AS_FILEINFO |
        FilesystemIterator::SKIP_DOTS
    ),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($rit as $path => $fileinfo) {
    $level = $rit->getDepth();
    if ($fileinfo->isDir()) {
        print "[{$level}] in dir:{$path}\n";
    } else {
        print "[{$level}] file: {$path}\n";
    }
}

This might look a little scary, but it's actually relative straightforward:

  1. The RecursiveDirectoryIterator will do the bulk of the work, however if you iterate over it you will get the selected directory's nodes as you would get by a a simple DirectoryIterator.

  2. However since it implements the RecursiveIterator interface, you can drive it by a RecursiveIteratorIterator and save yourself the "has child? recurse into ... " conditionals in your loops, this is what the RecursiveIteratorIterator will do for you and give back an iterator that looks flat, but it actually recurses into the child nodes. You can use the getDepth to detect when a sub-iteration with leaf nodes starts, and you can use the full path name to detect how the directories are changing.

complex857
  • 20,425
  • 6
  • 51
  • 54