How can the code for file uploads be improved in terms of cleanliness and readability?

The code for file uploads can be improved in terms of cleanliness and readability by breaking it down into smaller, more modular functions. This helps to separate concerns and make the code easier to understand and maintain. Additionally, using meaningful variable names and comments can also enhance the readability of the code.

<?php

function uploadFile($file) {
    if ($file['error'] !== UPLOAD_ERR_OK) {
        // Handle file upload error
        return false;
    }

    $uploadDir = 'uploads/';
    $uploadPath = $uploadDir . basename($file['name']);

    if (move_uploaded_file($file['tmp_name'], $uploadPath)) {
        return $uploadPath;
    } else {
        // Handle file move error
        return false;
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $uploadedFile = $_FILES['file'];

    $uploadedFilePath = uploadFile($uploadedFile);

    if ($uploadedFilePath) {
        echo 'File uploaded successfully: ' . $uploadedFilePath;
    } else {
        echo 'File upload failed.';
    }
}
?>