title: Check multiple levels of folders is dir
body: <p>When user types in a new folder name and the name all ready exists in sub folders and main folder then will return error.</p>
  
  <p>Currently it returns error even if folder not exist in sub folders etc.</p>
  
  <blockquote>
    <p><strong>Question</strong> How can I make it so that it can check main directory and sub folders and return error if exits else lets me create it.</p>
  </blockquote>
  
  <p>The post for foldername is  <code>$this-&gt;input-&gt;post('folder')</code></p>
  
  <p>Checks if any subfolders exists with same name.</p>
  
  <pre><code>if (!$json) {
  
      $results = scandir($DIR_IMAGE);
  
      foreach ($results as $result) {
          if ($result === '.' or $result === '..') continue;
  
          if (is_dir($DIR_IMAGE . '/' . $result)) {
              $json['error'] = 'This folder name all ready';
          }
      }
  }
  </code></pre>
  
  <p>Full Function</p>
  
  <pre><code>public function folder() {
  
      $json = array();
  
      $DIR_IMAGE = FCPATH . 'images/';
  
      if ($this-&gt;input-&gt;get('directory')) {
          $directory =  $this-&gt;input-&gt;get('directory') . '/';
      } else {
          $directory = 'catalog/';
      }
  
      if (!$json) {
          $arrPathParts = explode('/', $directory);
  
          if (count($arrPathParts) &gt; 3) {
              $json['error'] = 'You can not create a new folder here try back one level.';
          }
      }
  
      if (!$json) {
  
          $re = '/\W/'; // \W matches any non-word character (equal to [^a-zA-Z0-9_])
  
          $str = $this-&gt;input-&gt;post('folder');
  
          $is_correct_foldername = !preg_match($re, $str) ? true : false; 
  
          if (!$is_correct_foldername) {
              $json['error'] = 'You have some symbols or no spaces that are not allowed';
          }
      }
  
      // Checks if any subfolders exists with same name.
  
      if (!$json) {
  
          $results = scandir($DIR_IMAGE);
  
          foreach ($results as $result) {
              if ($result === '.' or $result === '..') continue;
  
              if (is_dir($DIR_IMAGE . '/' . $result)) {
                  $json['error'] = 'This folder name all ready';
              }
          }
      }
  
      // Every thing passes now will create folder
      if (!$json) {
          mkdir($DIR_IMAGE . $directory . $this-&gt;input-&gt;post('folder'), 0777, true);
          $json['success'] = 'Your folder is now created!';
      }
  
      $this-&gt;output-&gt;set_content_type('Content-Type: application/json');
      $this-&gt;output-&gt;set_output(json_encode($json));        
  }
  </code></pre>