Amit K.
Amit K.

Reputation: 3754

How to convert directory structure to array of urls

I want to convert my directory structure to array format with file urls. Here is my directory structure.

public
  |-product_001
      |-documents
      |   |- doc_001.txt
      |   |- doc_002.txt
      |   |- doc_003.txt
      |
      |-gallery
          |- img_001.png
          |- img_002.png
          |- img_003.png

And this is what i want:

array(
  'product_001' =>array(
      'documents' =>array(
         0 => "public/product_001/documents/doc_001.txt",
         1 => "public/product_001/documents/doc_002.txt",
         2 => "public/product_001/documents/doc_003.txt"
      )
      'gallery' =>array(
         0 => "public/product_001/gallery/img_001.png",
         1 => "public/product_001/gallery/img_002.png",
         2 => "public/product_001/gallery/img_003.png"
      )
  )
)

Here is function:

function dirToArray($dir,$url) {

    $result = array();

    $cdir = scandir($dir);
    foreach ($cdir as $key => $value) {

        if (!in_array($value, array(".", ".."))) {
            if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
                $url.=DIRECTORY_SEPARATOR.$value;
                $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value,$url);
            } else {
                $result[] = $url.DIRECTORY_SEPARATOR.$value;
            }
        }
    }

    return $result;
}

Here is output i got till now:

Array
(
    [product_001] => Array
        (
            [documents] => Array
                (
                    [0] => public/product_001/documents/doc_001.txt
                    [1] => public/product_001/documents/doc_002.txt
                    [2] => public/product_001/documents/doc_003.txt
                )

            [gallery] => Array
                (
               [0] => public/product_001/documents/gallery/img_001.png
               [1] => public/product_001/documents/gallery/img_002.png
               [2] => public/product_001/documents/gallery/img_003.png
                )

        )

)

Can any one help me to achieve this? Many Thanks in advance.

Upvotes: 4

Views: 244

Answers (1)

E_p
E_p

Reputation: 3144

Should be even easier. Usually if you have a recursion you do not need state. So just get read of yours $url and clean up code no need to do concatenations multiple times.

As per Ryan Vincents' comment added dynamic separators.

As per Mavericks' comment added root parameter.

<?php

function dirToArray($dir, $separator = DIRECTORY_SEPARATOR, $root = '') {

    $result = array();
    if ($root === '') {
        $root = $dir;
    }

    $cdir = scandir($dir);
    foreach ($cdir as $key => $value) {

        if (!in_array($value, array(".", ".."))) {
            $current = $dir . $separator . $value;

            if (is_dir($current)) {
                $result[$value] = dirToArray($current, $separator, $root);
            } else {
                $result[] = str_replace($root, '',$current);
            }
        }
    }

    return $result;
}

Upvotes: 4

Related Questions