What is the best way to prevent uploading images wider than 500 pixels in PHP?

To prevent uploading images wider than 500 pixels in PHP, you can check the width of the uploaded image using the `getimagesize()` function and compare it to the maximum allowed width. If the width exceeds 500 pixels, you can prevent the image from being uploaded by displaying an error message to the user.

// Check if the uploaded file is an image
if(isset($_FILES['image']['tmp_name'])){
    $image_info = getimagesize($_FILES['image']['tmp_name']);
    $image_width = $image_info[0];
    
    // Check if the image width is greater than 500 pixels
    if($image_width > 500){
        echo "Error: Image width must be 500 pixels or less.";
    } else {
        // Proceed with uploading the image
        move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $_FILES['image']['name']);
        echo "Image uploaded successfully.";
    }
}