What role does XHR (XMLHttpRequest) play in handling file upload progress updates in PHP applications?

XHR (XMLHttpRequest) plays a crucial role in handling file upload progress updates in PHP applications by allowing the client-side JavaScript to send asynchronous requests to the server and receive progress updates during the file upload process. This enables the PHP server to track the progress of the file upload and send back updates to the client in real-time.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $file = $_FILES['file'];
    
    $uploadPath = 'uploads/' . $file['name'];
    
    move_uploaded_file($file['tmp_name'], $uploadPath);
    
    echo json_encode(['status' => 'success', 'message' => 'File uploaded successfully']);
    exit;
}
?>