PHP Force Download with Custom Filename

If you want to serve up text files for download (perhaps product feeds etc) then you might like this little snippet.

Not only will it force the file to be downloaded but it allows you to specify a custom filename that it should be saved as.

if(isset($_GET['download_file'])){
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=Export.txt");
    readfile('Export.txt');
    die;
}

Tags: phpcustomfilesavefeeddownloadtextheaderforceasfilenametxtexport