What potential pitfalls should be considered when using user-uploaded images in a PHP script for a gallery?

One potential pitfall when using user-uploaded images in a PHP script for a gallery is the risk of malicious files being uploaded, such as scripts that could harm your server or compromise user data. To mitigate this risk, you should validate the uploaded files to ensure they are actually images and not harmful scripts. One way to do this is by checking the file extension and using PHP's `getimagesize()` function to verify the file type.

// Validate uploaded image file
$uploadFile = $_FILES['image']['tmp_name'];
$imageInfo = getimagesize($uploadFile);
if($imageInfo === false) {
    // File is not a valid image
    die('Invalid image file.');
}

// Continue with image processing and storage
// Your code for handling the uploaded image goes here