What are common pitfalls when trying to download files from URLs listed in a text file using PHP?

Common pitfalls when trying to download files from URLs listed in a text file using PHP include not properly handling errors, not sanitizing input data, and not checking for valid URLs. To solve these issues, it is important to validate each URL, handle exceptions, and ensure the file is downloaded successfully.

<?php
$file = 'urls.txt';
$urls = file($file, FILE_IGNORE_NEW_LINES);

foreach ($urls as $url) {
    if (filter_var($url, FILTER_VALIDATE_URL)) {
        $file_name = basename($url);
        $file_path = 'downloads/' . $file_name;
        
        $file_content = file_get_contents($url);
        
        if ($file_content !== false) {
            file_put_contents($file_path, $file_content);
            echo "File downloaded successfully: $file_name <br>";
        } else {
            echo "Error downloading file: $url <br>";
        }
    } else {
        echo "Invalid URL: $url <br>";
    }
}
?>