Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
session_start();
function uploadImage() {
$target_dir = "imageupload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check === false) {
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 15000000) {
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$uploadOk = 0;
}
if ($uploadOk == 0) {
return null;
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
return $target_file;
} else {
return null;
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if a message has already been sent in this session
if (!isset($_SESSION['message_sent'])) {
$name = htmlentities($_POST['name']);
$message = htmlentities($_POST['message']);
$timestamp = time();
$formattedDate = date('H:i | d-m-y', $timestamp);
$picture = null;
if(isset($_FILES['fileToUpload']) && $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) {
$picture = uploadImage();
}
$new_messages = array(
'name' => htmlentities($name),
'message' => htmlentities($message),
'timestamp' => $formattedDate,
'imagepath' => $picture
);
$json_data = file_get_contents('messageData.json');
$assoc_data = json_decode($json_data, true);
$assoc_data[] = $new_messages;
file_put_contents('messageData.json', json_encode($assoc_data, JSON_PRETTY_PRINT));
// Mark message as sent in this session
$_SESSION['message_sent'] = true;
header('Location: messages.php');
exit(); // It's a good practice to exit after a redirect
} else {
// Message has already been sent in this session, do not process again
// Optionally, you can redirect the user to some other page or display a message
header('Location: messages.php?error=already_sent');
exit();
}
}
?>