What are some common mistakes to avoid when uploading and displaying images in PHP?

One common mistake to avoid when uploading and displaying images in PHP is not validating the file type before allowing it to be uploaded. This can lead to security vulnerabilities such as allowing users to upload malicious files. To prevent this, always validate the file type before processing the upload.

// Validate file type before uploading
$allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
$file_extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);

if (!in_array($file_extension, $allowed_extensions)) {
    echo "Invalid file type. Please upload a JPG, JPEG, PNG, or GIF file.";
    exit;
}

// Process the upload
// Your upload code here