What are common issues that can arise when using PHP scripts to download files, and how can they be resolved?
Issue: One common issue when using PHP scripts to download files is that the downloaded file may be corrupted or incomplete due to improper handling of headers or content length. This can be resolved by setting the correct headers and ensuring the content length is accurate. Code snippet:
<?php
$file = 'example.txt';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>