<?php 
require("functions.php");
$target_dir = "../img/catalog/";
$imageFileType = pathinfo($_FILES['CatalogItemImage']['name'],PATHINFO_EXTENSION);
$target_file = $target_dir . $_POST['CatalogItemLine'] . " " . $_POST['CatalogItemSKU'] . "." . "jpg";
$uploadOk = 1;


// Check if image file is a actual image or fake image
if (strtolower($imageFileType) != "jpg" && strtolower($imageFileType) != "png") {
	$data['error'] = "This image is not valid, at least does not have the .jpg or .png extension.";
}else{
	if (move_uploaded_file($_FILES["CatalogItemImage"]["tmp_name"], $target_file)) {
	        $data['message'] = "The file ". basename( $_FILES["CatalogItemImage"]["name"]). " has been uploaded and renamed to \"" . $_POST['CatalogItemLine'] . " " . $_POST['CatalogItemSKU'] . "." . strtolower("jpg") . "\"";
	        $data['url'] = "$target_dir" . $_POST['CatalogItemLine'] . " " . $_POST['CatalogItemSKU'] . "." . "jpg";
	        $data['fileuploaded'] = true;
	    } else {
	        $data['error'] = "Sorry, there was an error uploading your file.";
	    }
}

header('Content-Type: application/json');

echo json_encode($data);

?>