Reputation: 135
I'm developing a web application.
A user can download a specific zip file using readfile() if the zip has been correctly generated.
But since I've integrated the readfile() line, I can't echo anything before or after this line. Here is the code sample causing problem :
if($zip->status == 0){
$zip->close();
$file_url = './zip/'.$userDir.'.zip';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
echo'
<div class="alert alert-primary" role="alert">
Success
</div>';
}
Everything in the condition is called except for the echo lines at the end. When I remove the readfile($file_url) line, the echo is called.
I also tried to move the echo lines before the line
$file_url = './zip/'.$userDir.'.zip';
And it doesn't work either. Am I missing something ?
Upvotes: 0
Views: 1337
Reputation: 449
I had a similar problem. In my case, a submitted form is generating a pdf file but since automatic download is initiated with readfile() on the action page, HTML of the latter wouldn't load, the browser keeps the form page open - that's where I added an overlay div which becomes visible only after clicking submit, blocking all content except a simple button which takes to the same action page but with the help of GET information, readfile() code isn't running again. Sure a button has to be clicked but it can be a good thing, some sort of download acknowledgement.
It feels, however, a bit odd, loading the same page twice.
Upvotes: 0
Reputation: 2621
If you echo after "sending the file" to the user you are going to corrupt the file.
You have to think as the php code as a download link, the user clicks on it and get a file. Also sending an echo is not going to the user screen since you have this headers
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
Upvotes: 1