Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:25:58+00:00 2026-05-26T23:25:58+00:00

I’ve been banging my head against this one all day long. Time to ask

  • 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)

PAGE START
if(post[getorder]){

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

$ctype="application/force-download";
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 );
flush();
}
fclose($handle);    
$successmessage = "Your download has begun";                  
}

if($successmessage){
 echo $successmessage;
}

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

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:25:59+00:00Added an answer on May 26, 2026 at 11:25 pm

    On the download landing page, try adding <meta http-equiv="refresh" content="1; url=/download/file/url"> instead of using headers. This will make the browser load the page, then after 1 second redirect to the file download.

    Additionally, if you are serving anything other than small files, I’d recommend that you check out X-Sendfile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.