What are some potential pitfalls to consider when allowing users to upload images via a PHP form?

One potential pitfall when allowing users to upload images via a PHP form is the risk of malicious files being uploaded, such as scripts that could harm your server or execute unauthorized actions. To mitigate this risk, you can validate the uploaded file type and restrict it to only allow certain image formats like JPG, PNG, or GIF.

// Validate uploaded file type
$allowedFormats = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedFile = $_FILES['image']['name'];
$ext = pathinfo($uploadedFile, PATHINFO_EXTENSION);

if (!in_array(strtolower($ext), $allowedFormats)) {
    // Display an error message and prevent further processing
    echo "Only JPG, PNG, and GIF files are allowed.";
    exit;
}