title: Convert Recursive Directory Listing to MySQL Table
body: <p>I need to list all files/folders in a given parent folder, and dump it out to mysql.</p>
  
  <p>So far I have:</p>
  
  <pre><code>&lt;?php
  
  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
  error_reporting(E_ALL);
  
  $dir = '/home/kevinpirnie/www';
  
  function dirToArray( $dir ) {
      $result = array();
      $cdir = scandir($dir);
      foreach ($cdir as $key =&gt; $value) {
          if (!in_array($value, array(".", "..")))  {
              if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){
                  $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
              } else {
                  $result[] = $value;
              }
          }
      }
      return $result;
  }
  
  $res = dirToArray($dir);
  
  echo '&lt;hr /&gt;';
  echo '&lt;pre&gt;';
  print_r($res);
  echo '&lt;/pre&gt;';
  </code></pre>
  
  <p>What I am stuck on is how I can assign <code>ID</code>'s to the directories, and then associate them with their parent <code>ID</code>'s.</p>
  
  <p>Right now, this code sort of does what I need it to, I just need to be able to convert it to mysql insert statements, yet keep the associative structure, and I am braindead from a long long week of work.</p>
  
  <p>In the end, I am looking to have a table structure similar to:</p>
  
  <p>FileID, FolderID, ParentFolderID, FileName</p>
  
  <p>How can I do this?</p>