What are the best practices for handling file uploads in PHP, specifically when using the $_FILES array?

When handling file uploads in PHP using the $_FILES array, it is important to validate the file type, size, and ensure proper security measures are in place to prevent malicious uploads. One common best practice is to move the uploaded file to a secure directory on the server and assign it a unique name to prevent overwriting existing files.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $uploadDir = 'uploads/';
    $uploadFile = $uploadDir . basename($_FILES['file']['name']);

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
        echo "File is valid, and was successfully uploaded.";
    } else {
        echo "Upload failed";
    }
}
?>