Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file download with headers

I have problem with my PHP code:

header('Content-Disposition: attachment; filename="config.yml"');
header('Content-Type: text/plain');
header('Content-Length: '.strlen($file));
readfile($file);

File is downloaded well, but page will close instantly. Like downloading will start, and page will close. But I have in page also some else code, like html. I don't want to close page after downloading. Can you give me some advice ? Because I don't know, where can be mistake.

like image 547
user3870228 Avatar asked Jul 10 '26 08:07

user3870228


1 Answers

You cannot return both the file download and html or other content in the same response.

What you need to do is return the html first and within that have a request for the download. E.g.

<iframe width="1" height="1" frameborder="0" src="/downloadfile.php"></iframe>
like image 175
Steve E. Avatar answered Jul 13 '26 10:07

Steve E.