title: Dividing Up CodeIgniter Multi-File DropZone Uploader Code into MVC?
body: <p>I am fairly new to CodeIgniter and I am trying to convert a working <a href="http://www.dropzonejs.com/" rel="nofollow noreferrer">DropZone</a> multi-file uploader to the MVC CodeIgniter format. I have seen some other examples here but need to get recommendations for my specific code. </p>
  
  <p>I know how to place the upload/form page in a view and load via a controller.  However, the form handler code is where I need help.</p>
  
  <p>Upload/Form Page:</p>
  
  <pre><code>&lt;head&gt;
  &lt;script type="text/javascript" src="js/jquery-2.1.1.js"&gt;&lt;/script&gt;
  &lt;!-- Add Dropzone --&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/dropzone.css" /&gt;
  &lt;script type="text/javascript" src="js/dropzone.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="image_upload_div"&gt;
  &lt;form action="upload_thumbnails.php" class="dropzone"&gt;
  &lt;/form&gt;
  &lt;/div&gt;  
  
  &lt;script type="text/javascript"&gt;
  //Disabling autoDiscover
  Dropzone.autoDiscover = false;
  $(function() {
      //Dropzone class
      var myDropzone = new Dropzone(".dropzone");
      myDropzone.on("queuecomplete", function() {
          //Redirect URL
          //window.location.href = 'http://php.net';
      });
  });
  &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>What parts of the form handler code should go in the model vs controller?  This is the section I am struggling with.  </p>
  
  <p>Form Handler Code:</p>
  
  <pre><code>if(!empty($_FILES)){    
  
    function createThumbnail($filename) {
      $final_width_of_image = 200;
      $path_to_image_directory = 'uploads/';
      $path_to_thumbs_directory = 'uploads/thumbs/';
  
      if(preg_match('/[.](jpg)$/', $filename)) {
          $im = imagecreatefromjpeg($path_to_image_directory . $filename);
      } else if (preg_match('/[.](gif)$/', $filename)) {
          $im = imagecreatefromgif($path_to_image_directory . $filename);
      } else if (preg_match('/[.](png)$/', $filename)) {
          $im = imagecreatefrompng($path_to_image_directory . $filename);
      }
  
      $ox = imagesx($im);
      $oy = imagesy($im);
      $nx = $final_width_of_image;
      $ny = floor($oy * ($final_width_of_image / $ox));
      $nm = imagecreatetruecolor($nx, $ny);
      if(!imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy)){
          header("HTTP/1.0 500 Internal Server Error");
          echo 'Thumbnail Not created';
          exit(); 
      }
      if(!file_exists($path_to_thumbs_directory)) {
          if(!mkdir($path_to_thumbs_directory)) {
              header("HTTP/1.0 500 Internal Server Error");
              echo 'Thumbnail Not Created';
              exit();
          } 
      }
      // Save new thumbnail image
      imagejpeg($nm, $path_to_thumbs_directory . $filename);
  
    }
  
  //database configuration
  $dbHost = 'localhost';
  $dbUsername = 'insertnamehere';
  $dbPassword = 'insertpwhere';
  $dbName = 'ci_local';
  //connect with the database
  $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  if($mysqli-&gt;connect_errno){
      echo "Failed to connect to MySQL: (" . $mysqli-&gt;connect_errno . ") " . $mysqli-&gt;connect_error;
  }
  
  $targetDir = "uploads/";
  $fileName = $_FILES['file']['name'];
  $targetFile = $targetDir.$fileName;
  
  if(move_uploaded_file($_FILES['file']['tmp_name'],$targetFile)){
      // NEW
      //createThumbnail($file['file']['name']);
      createThumbnail($fileName);
      // NEW
  
      //insert file information into db table
      $conn-&gt;query("INSERT INTO files (file_name, uploaded) VALUES('".$fileName."','".date("Y-m-d H:i:s")."')");
  } else {
      echo "Ooops";
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any recommendations will be greatly appreciated.</p>