0

Perhaps there's a better method than the PHP route, I am open to ideas.

The problem: I have a folder structure for each user. This folder may contain files or sub-folders. I am only concerned with the folders which have files. I am able to run a query and get a recursive listing on a multidimensional array (from PHP code posted on the web). The issue is that I get a multidimensional array that when flattened only lists the inner most file name, and I need to store the path and file -or- the path+file in my sql dbase. The idea is to allow the user to view his files and delete them using a web interface.

Here is an example result from the PHP recursive function. Inside a folder named "Jimmy", you find:

Array
(
[0] => info.txt
[1] => log.tmp
[2] => README.md
[css] => Array
    (
        [0] => style.css
    )

[images] => Array
    (
        [0] => flower.gif
    )
[3] => index.php
[testDir] => Array
    (
        [anotherTestDir] => Array
            (
                [0] => test2.php
            )

    )
[5] => listing.txt
[temp] => Array
    (
    )

)

What I'd wold like to see is this:

/jimmy/info.txt
/jimmy/log.tmp
/jimmy/README.md
/jimmy/css/style.css
/jimmy/images/flower.gif
/jimmy/index.php
/jimmy/testDir/anotherTestDir/test2.php
/jimmy/listing.txt

And then I would need to sort the string and add it to mysql dbase. Any help on how to achieve this effect would be much appreciated. Once I have my flatten array i want to add it in one go to the user's "folder" table.

2
  • 1
    Sounds like you need a tree traversing recursive function that keeps track of what the contents of the tree has been so far (send the path built so far to each child node so it can build its version of the path, send that to its child, etc) and passes an array through the entire traversal to get populated by every leaf node result. Commented Apr 22, 2013 at 23:32
  • What do you see now? with what code? Commented Apr 22, 2013 at 23:40

2 Answers 2

3
<?php
$d = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/dir'));
foreach($d as $file){
        if($file->isFile()) echo $d->getSubPathname().PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you actually read the question, these "files" are not on a local filesystem (or, any filesystem) but in a virtual tree stored in a database, so the Iterator example is not directly useful in this case.

You could however use that example if you write a file stream wrapper for your database structure. But even then you'd be stuck on a tough recursive SQL query (to make your database server 'build' you the strings in a computed field, which is what I came here hoping to find...), or lots of CPU time in PHP collating through the structure to generate the "long name" of each file or folder.

However if you just want to work with the data as input, you could use a loop and some is_numeric() or is_string() logic (though you can't have a directory that is numeric then, which may or may not be an issue)... or detect the leafs by the fact they have no array subchildren and then you know that's a 'file' and the parents are all 'folders' regardless of the array index "data type". For recursing arrays like that I usually use by-reference loops, especially if I will be modifying nodes (or adding elements within the tree such as a 'full_path' field for each node). There are some good examples of this on the PHP doc site for the page about references and dereferencing. Also if your data set will never be large this may work acceptably. In my project it won't, we've already hit some resource limits and it's taking too long to process within each 'hit' to our file manager interface. So I strongly urge making the database do the heavy lifting and string concatenations (and then tell me how you did it!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.