What are common reasons for images not being uploaded successfully in PHP scripts?

Common reasons for images not being uploaded successfully in PHP scripts include incorrect file permissions, exceeding server upload limits, or not properly handling file types. To solve these issues, ensure that the upload directory has the correct permissions, adjust the server's upload_max_filesize and post_max_size settings in php.ini, and validate the file type before attempting to upload it.

// Example code for handling image upload in PHP
if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
    $uploadDir = 'uploads/';
    $uploadFile = $uploadDir . basename($_FILES['image']['name']);
    
    // Validate file type
    $imageFileType = strtolower(pathinfo($uploadFile, PATHINFO_EXTENSION));
    if ($imageFileType != 'jpg' && $imageFileType != 'png' && $imageFileType != 'jpeg') {
        echo 'Invalid file type. Only JPG, JPEG, and PNG files are allowed.';
    } else {
        if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadFile)) {
            echo 'Image uploaded successfully.';
        } else {
            echo 'Failed to upload image.';
        }
    }
} else {
    echo 'Error uploading image.';
}