Why are 0 byte files being downloaded in the PHP script?

The issue of 0 byte files being downloaded in the PHP script may be due to incorrect file paths or permissions. To solve this issue, ensure that the file path is correct and that the file has proper read permissions set. Additionally, check if the file actually contains data before attempting to download it.

<?php
$file = 'path/to/file.txt';

if (file_exists($file) && filesize($file) > 0) {
    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;
} else {
    echo 'File not found or empty.';
}
?>