Ivan
Ivan

Reputation: 5248

Insert directory name and files in db

Hello guys i dont know how to insert array to db. I have function wich scan my directory and subdir and their files.

In my db i need to write dir name and file name.

ID |  dir_name | file_name 
------------------------------
1  |  album_1  | Hero-527920799.jpg

For scanning dir i use this function:

 function scanDirectory($dir) {

   $result = array();

   $cdir = scandir($dir);
   foreach ($cdir as $key => $value)
   {
      if (!in_array($value,array(".","..")))
      {
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
         {
            $result[$value] = scanDirectory($dir . DIRECTORY_SEPARATOR . $value);
         }
         else
         {
            $result[] = $value;
         }
      }
   }

   return $result;
} 

How now all that directories and files to pull in db from foreach or while loop ?

Array
(
    [album-1] => Array
        (
            [0] => Hero-527920799.jpg
        )

    [album-2] => Array
        (
            [0] => topic-1350661050.jpg
        )

    [album-3] => Array
        (
            [0] => bigcompany.jpg
        )

    [album-4] => Array
        (
            [0] => small_animal.jpg
        )

    [album-5] => Array
        (
            [0] => little_animals.jpg
        )

)

Upvotes: 0

Views: 1048

Answers (1)

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

The first approach is at the end of the post. Set $data to be the result of scanDirectory($dir) call.

Approach 2:

$data = scanDirectory($dir);
$result = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data), RecursiveIteratorIterator::LEAVES_ONLY);
foreach($iterator as $key => $value) {
    $parent_key = $iterator->getSubIterator($key)->key();
    $result[$parent_key][] = $value;
}

foreach($result as $key => $value) {
    foreach($value as $s_key => $s_value) {
        $sql = "INSERT INTO your_table (dir_name, file_name) VALUES ('$key', '$s_value')";
        //Execute $sql here
    }
}

Approach 3:

If this is your data structure then you can use the following:

$data = array(
    'album-1'=>array(
        'Hero-527920799.jpg',
        'Hero2-527920799.jpg',
    ),
    'album-2'=>array(
        'topic-1350661050.jpg',
        'topic2-1350661050.jpg',
    ),
    'album-3'=>array(
        'bigcompany.jpg',
        'topic2-1350661050.jpg',
    ),
);

foreach($data as $key => $value) {
    foreach($value as $s_key => $s_value) {
        $sql = "INSERT INTO your_table (dir_name, file_name) VALUES ('$key', '$s_value')";
        echo $sql.'<br />';
        //Execute $sql here
    }
}

Result:

INSERT INTO your_table (dir_name, file_name) VALUES ('album-1', 'Hero-527920799.jpg')
INSERT INTO your_table (dir_name, file_name) VALUES ('album-1', 'Hero2-527920799.jpg')
INSERT INTO your_table (dir_name, file_name) VALUES ('album-2', 'topic-1350661050.jpg')
INSERT INTO your_table (dir_name, file_name) VALUES ('album-2', 'topic2-1350661050.jpg')
INSERT INTO your_table (dir_name, file_name) VALUES ('album-3', 'bigcompany.jpg')
INSERT INTO your_table (dir_name, file_name) VALUES ('album-3', 'topic2-1350661050.jpg')

First approach (not working):

function gatherValue($value, $key, $data) {
    $data[$key][] = $value;
}

$result = new ArrayObject();
array_walk_recursive($data, 'gatherValue', $result);

foreach($result as $key => $value) {
    $sql = "INSERT INTO your_table (dir_name, file_name) VALUES ('$key', '$value')";
}

Upvotes: 1

Related Questions