What are common issues when using PHP to handle file downloads?
One common issue when using PHP to handle file downloads is that the file may not be downloaded correctly due to improper headers being set. To solve this, make sure to set the correct headers before outputting the file content.
// Set the appropriate headers for file download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.txt"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('example.txt'));
// Output the file content
readfile('example.txt');