I am fairly new to CodeIgniter and I am trying to convert a working DropZone multi-file uploader to the MVC CodeIgniter format. I have seen some other examples here but need to get recommendations for my specific code.
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.
Upload/Form Page:
<head>
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<!-- Add Dropzone -->
<link rel="stylesheet" type="text/css" href="css/dropzone.css" />
<script type="text/javascript" src="js/dropzone.js"></script>
</head>
<body>
<div class="image_upload_div">
<form action="upload_thumbnails.php" class="dropzone">
</form>
</div>
<script type="text/javascript">
//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';
});
});
</script>
</body>
What parts of the form handler code should go in the model vs controller? This is the section I am struggling with.
Form Handler 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->connect_errno){
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->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->query("INSERT INTO files (file_name, uploaded) VALUES('".$fileName."','".date("Y-m-d H:i:s")."')");
} else {
echo "Ooops";
}
}
Any recommendations will be greatly appreciated.