How can PHP be used to display a progress indicator while generating multiple HTML files in the background?

When generating multiple HTML files in the background, it's important to provide a progress indicator to keep users informed about the process. One way to achieve this is by using AJAX requests to periodically check the status of the file generation process and display a progress bar or message accordingly.

<?php
// Start background process to generate HTML files
// This is a placeholder for the actual code to generate HTML files in the background

// Return a unique identifier for the background process
$process_id = uniqid();

// AJAX endpoint to check the status of the background process
echo "<div id='progress'></div>";
echo "<script>
    function checkProgress() {
        $.ajax({
            url: 'check_progress.php?process_id=$process_id',
            success: function(data) {
                $('#progress').html(data);
                if (data != '100%') {
                    setTimeout(checkProgress, 1000); // Check progress every 1 second
                }
            }
        });
    }
    checkProgress();
</script>";

// Check the status of the background process
if (isset($_GET['process_id'])) {
    $process_id = $_GET['process_id'];
    // This is a placeholder for the actual code to check the status of the background process
    $progress = rand(0, 100); // Simulate progress percentage
    echo $progress . '%';
}
?>