projecte_ionic_entrega_02/backend/insert_old.php

58 lines
1.8 KiB
PHP

<?php
include "config.php";
$response = array();
$name = $_POST["countryName"];
$continent = $_POST["countryContinent"];
$area = $_POST["countryArea"];
$population = $_POST["countryPopulation"];
$drivingSide = $_POST["countryDrivingSide"];
$flag = $_FILES["countryFlag"];
$target_dir = "assets/img/flags/";
$imageType = strtolower(pathinfo($flag['name'],PATHINFO_EXTENSION));
$file_name = fileNamer() . "." . $imageType;
$target_file = $target_dir . $file_name;
$uploadOk = 1;
// Check if image file is a actual image or fake image
if(true) {
$check = getimagesize($flag["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
} else {
echo "File is not an image.";
$uploadOk = 0;
}
if (move_uploaded_file($flag["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $flag["name"])). " has been uploaded.";
$response["message"] = "OK";
} else {
echo "Sorry, there was an error uploading your file.";
$uploadOk = 0;
}
}
$sqlString = "INSERT INTO cs_countries (cs_country_name, cs_country_continent, cs_country_area, cs_country_population, cs_country_drivingSide, cs_country_flag)";
$sqlString .= " VALUES ('$name', '$continent', $area, $population, '$drivingSide', '$file_name')";
$sql = mysqli_query($con, $sqlString);
if($sql){
$response["message"] = "OK";
}else{
$response["message"] = "KO";
}
function fileNamer() {
$today = date('Ymd');
$range = $today - $startDate;
$rand = rand(1000, 9999);
return $today . "-" . $rand;
}
echo json_encode($response);
?>