0

I'd like to loop through 4 paths, and for each path store that specific path's files in an array. The reason is, I'd like to insert each file title and file path in a database.

So:

$COMICS_path = array('./images/Comics/Charts/', './images/Comics/Life/', './images/Comics/Misc/', './images/Comics/Office/', './images/Comics/Political/');

$COMICS_files = array(scandir('./images/Comics/Charts/'), scandir('./images/Comics/Life/'), scandir('./images/Comics/Misc/'), scandir('./images/Comics/Office/'), scandir('./images/Comics/Political/'));

$COMICS_path seems to output the correct array.

but, $COMICS_files just outputs "ARRAY", when I'm expecting something like:

$COMICS_files = (file1.png, file2.png, file3.png, ... file n.png)

Is this possible? If not, can anyone direct me to the best way to loop through several folders and retrieve each file?

Thanks!

2
  • 2
    It will output Array if you echo it or attempt to use the array as a string (via concatenation, etc). print_r($COMICS_files) to see what's really in there. Commented Mar 11, 2013 at 18:43
  • Look into array_merge. Commented Mar 11, 2013 at 18:44

3 Answers 3

1
$COMICS_files = array();
foreach($COMICS_path as $path)
    $COMICS_files = array_merge($COMICS_files, scandir($path));
var_dump($COMICS_files);

Since scandir() returns an array, you would have to merge them together into one big array.

Sign up to request clarification or add additional context in comments.

Comments

1

It's outputting a 2 dimensional array because scandir returns an array.

So $COMICS_files[0] will be (afile1.png, afile2.png, ...) and $COMICS_FILES[1] will be (bfile2.png, bfile2.png)

If you want all of these files in a single array you should do something like so

$COMICS_path = array('./images/Comics/Charts/', './images/Comics/Life/', './images/Comics/Misc/', './images/Comics/Office/', './images/Comics/Political/');
$COMICS_files = array();

$COMICS_path_sz = count($COMICS_path);
for ($i = 0; $i < $COMICS_path_sz; ++$i) {
    $tmp_arr = scandir($COMICS_path[i]);
    if ($tmp_arr !== FALSE) {
        $COMICS_files = array_merge($COMICS_files, $tmp_arr);
    }
}

Comments

1

First of all, we need to sort out what is the "output" you are referring to. If you refer to the results of echo, then the expected result is "ARRAY".

Secondly, as your "output" states, you have an array. You need to iterate through your array and insert your records.

A more elegant way to do that would be to generate all your inserts and send them at once to the database server, as the communication between your application server and database server is expensive, that is, it takes time.

2 Comments

How can I generate all of my inserts and send them at once?
If you have a command, such as "insert into Foo(Bar) values('foobar'); insert into Foo(Bar) values('barfoo');" and you send it at once to the database server, then you have a single request instead of two, so you don't have to wait twice for the message lag with the database server.

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.