What are some best practices for handling file uploads in PHP to ensure consistent functionality?

When handling file uploads in PHP, it is important to ensure consistent functionality by validating the file type, size, and ensuring proper error handling. One best practice is to use the move_uploaded_file() function to securely move the uploaded file to a designated directory on the server.

<?php
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    $uploadDir = 'uploads/';
    $uploadFile = $uploadDir . basename($_FILES['file']['name']);

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
        echo 'File uploaded successfully.';
    } else {
        echo 'Failed to move file.';
    }
} else {
    echo 'Error uploading file.';
}
?>