How can the use of exec() in PHP be leveraged to monitor and display download progress in real-time?
To monitor and display download progress in real-time using exec() in PHP, you can utilize a command-line tool like wget or curl that supports progress tracking. By capturing the output of the command using exec() and parsing it for progress information, you can then display this information to the user dynamically.
<?php
// Command to download a file with wget and display progress
$downloadUrl = "http://example.com/file.zip";
$command = "wget $downloadUrl --progress=bar:force 2>&1";
// Execute the command and capture output
exec($command, $output);
// Parse output for progress information
foreach ($output as $line) {
if (preg_match('/\d+%/', $line, $matches)) {
echo "Download Progress: " . $matches[0] . "\n";
}
}
?>