Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Header flow problems

trying to reload the page after a force-download

12:17 pm on Nov 21, 2011 (gmt 0)

New User

joined:July 14, 2011
posts: 19
votes: 0

I've been banging my head against this one all day long. Time to ask for some help.

So, I have a PHP script, which is a downloads page. Users have files they can download according to what they have ordered.

It goes something like this (not real code obviously, well some bits are - hopefully you can get the gist)


check order exists, get order filename etc from mysql;
update download attempts in db;

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
$handle = fopen($filename, "r");
while ( $contents = fread( $handle, 1024 ) )
print ( $contents );
$successmessage = "Your download has begun";

echo $successmessage;

get users order details from db;
display div
display form with post to post order number to script;
end form
end div

Hopefully that makes sense. I can put all the code if that would be more useful, but it's a long old script and I thought this might be more convenient.

So, if a user is just viewing the page, the script grabs the user's order information on any orders they might have. Users only get X attempts to download before the order is flagged complete and no longer available.

It displays a div per available order, and inside each div is a small form. If they click on the form, it posts the order number back to the same script at which point I'd hoped it would simply serve the file then continue displaying the rest of the page. Alas no, apparently the headers on the force download grab control and nothing happens after the fclose.

So I thought bugger it, and added a variable into the "if(post...)" along the lines of $sendfile = 1, and right at the bottom of the page...

if($sendfile == 1){ do headers stuff}

...hoping that the page would load up correctly before sending the file and everyone happy. Again, alas no. It serves the file first, and doesn't reload the page.

Again, hopefully my verbiage makes some sense.

I've read that a php script will 'do' the headers first, so doesn't matter where you put them. I don't know if that is true, but based upon what I am seeing, it is.

I've also tried using headers after the fclose to redirect to the same page, and use a simple GET to make the script display the success message. However if I do this, the reload works, but the download prompt does not display. Even if I put a sleep(10) before the redirect to self. It just waits and then reloads, but no file download.

The page re-load is quite important, because when the rest of the page re-gets the order data and displays it, the download attempts should be increased by 1.

Is there any way to achieve what I am trying to do? The code I am cutting is getting increasingly more hacky so any help or suggestions would be very much appreciated.
1:58 pm on Nov 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
votes: 0

I am not sure I understand why you are trying to do everything in the same request. Ok if you sent the download headers out you should not print anything other than the file and simply terminate the script once its complete. In other words you cannot redirect once you start sending - the file data - to the client end. Headers only work if they sent before anything else.

So now you want to print a message the download is complete but only after the whole file is echoed. You can use js that will initiate the download and basically the readystate tells if the "page" ie file-download is complete. In which case you do the redirect via js. I use jQuery for this, so I would wait on the ajaxsuccess and force the browser reload then. The server could store a success message after the download via the session which is echoed on the next page reload. So the php code you have with the headers and sending the file out stays as is and here is a reference.
You could of course use plain js.