How can the PHP script be modified to ensure that all files listed in the text file are downloaded successfully, instead of just the last one?
The issue is that the PHP script is overwriting the downloaded file each time it iterates through the loop, so only the last file is being saved. To ensure that all files listed in the text file are downloaded successfully, you can modify the script to create unique file names for each download by appending a timestamp or a unique identifier to the file name.
<?php
$file_list = file('files.txt');
foreach ($file_list as $file_url) {
$file_name = basename($file_url);
$timestamp = time(); // Generate a unique timestamp
$file_name_unique = $timestamp . '_' . $file_name; // Append timestamp to file name
file_put_contents($file_name_unique, file_get_contents(trim($file_url)));
echo "File downloaded: $file_name_unique <br>";
}
?>