What are some best practices for handling file uploads in PHP to avoid issues like "The page cannot be found" error?

When handling file uploads in PHP, it's important to ensure that the file upload settings in php.ini are correctly configured to allow for file uploads. Additionally, make sure to check the file size and file type before processing the upload to prevent any potential security vulnerabilities. Handling file uploads securely can help avoid issues like "The page cannot be found" error.

<?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";
    }
}
?>