What are common pitfalls when using the PHP function copy() for file downloads based on URLs?

Common pitfalls when using the PHP function copy() for file downloads based on URLs include not checking if the URL is accessible, not handling errors properly, and not setting appropriate permissions for the downloaded file. To solve these issues, you should first check if the URL is reachable, handle any errors that may occur during the download process, and ensure that the downloaded file has the correct permissions set.

$url = 'http://example.com/file.pdf';
$destination = 'downloads/file.pdf';

if (filter_var($url, FILTER_VALIDATE_URL)) {
    if (copy($url, $destination)) {
        echo 'File downloaded successfully.';
        chmod($destination, 0644); // Set appropriate permissions
    } else {
        echo 'Failed to download file.';
    }
} else {
    echo 'Invalid URL.';
}