Are there any security measures that should be implemented when creating a PHP guestbook script with image upload capability?

When creating a PHP guestbook script with image upload capability, it is important to implement security measures to prevent malicious file uploads or code injections. One way to enhance security is to validate the uploaded file type and restrict it to only allow specific image file formats such as JPEG, PNG, or GIF. Additionally, you should also consider implementing file size restrictions, sanitizing user input, and storing uploaded files in a secure directory outside of the web root.

// Validate uploaded file type
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['image']['type'], $allowedTypes)) {
    die('Invalid file type. Only JPEG, PNG, and GIF files are allowed.');
}

// Set file size limit
$maxFileSize = 2 * 1024 * 1024; // 2MB
if ($_FILES['image']['size'] > $maxFileSize) {
    die('File size exceeds limit. Maximum file size allowed is 2MB.');
}

// Sanitize user input
$comment = htmlspecialchars($_POST['comment']);

// Store uploaded file in a secure directory
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadFile)) {
    echo 'File uploaded successfully.';
} else {
    echo 'Error uploading file.';
}