What security considerations should be taken into account when creating an image gallery in PHP?

When creating an image gallery in PHP, it is important to consider security measures to prevent unauthorized access or malicious attacks. One common security consideration is to validate user input to prevent SQL injection and cross-site scripting (XSS) attacks. Additionally, it is important to properly handle file uploads to prevent malicious files from being uploaded to the server.

// Validate user input to prevent SQL injection and XSS attacks
$image_id = htmlspecialchars($_GET['image_id']);
$image_id = mysqli_real_escape_string($conn, $image_id);

// Handle file uploads securely
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        $uploadOk = 0;
    }
}