Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

http header large download fails

Status
Not open for further replies.

Kurt111780

Technical User
Nov 20, 2003
235
GB
Hello,

I have script that uses http headers for downloading files. It seems to work fine when on the same network but when outside the network downloading at 50kb/sec the download fails. Its a 22.0 MB file and it gets to about 3/4 the way through every time before it fails. Here is my header code.
Code:
   //Begin writing headers
   header("Pragma: private");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  // header("Cache-Control: public");
   header("Content-Description: File Transfer");
 
   //Use the switch-generated Content-Type
   //header("Content-Type: application/force-download");
   header("Content-Type: $ctype"); //FALSE
  // header("Content-Type: application/download", FALSE);

   //Force the download
   header("Content-Disposition: attachment; filename=\"" .basename($file) . "\"");
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".filesize($file));
   @readfile("$file");
   exit;
Please help,
Kurt

It's only easy when you know how.
 
The download fails in ie and firefox. In firefox it just stops after about 4 or 5 minutes. In ie it does the same but this error is displayed:

Internet explorer cannot download myFile.zip from The connection with the server was reset. It doesn't matter what type of file it is. It just seems to fail on large files.

Any Ideas?

Thanks

It's only easy when you know how.
 
It should take about 7 to 8 minutes to download the file and it fails after about 4 or 5 with just over 3 minutes remaining.

I tried adding this at the top of my script but it didn't seem to help

set_time_limit(6000);

Not sure if it matters but php is running on IIS.

Kurt

It's only easy when you know how.
 
set_time_limit(0) is actually the behavior you'd want (unlimited time)... but 6000 is about an hour forty minutes, so if that wasn't enough, unlimited probably won't be either.

Anyway, I actually found my old code, and I see I have alot of stuff commented out that you're using... I wonder if it caused problems for me...

If I'm not mistaken this is the one important switch though..

Code:
if ($fp = fopen($file, "rb"))
{
  fpassthru($fp);
  die;
}
else
{
  echo "ERROR: $file_to_pass";
}

And in case it isn't so simple, here's my complete code that was working on files up into the 150meg range

Code:
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
    {
      //trim out dots with html entitities to stop IE from being such a pain
      $filename = preg_replace('/\./', '%2e', $filename,
			       substr_count($filename, '.') - 1);
    }
  header("Cache-Control: ");
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=\"".trim(htmlentities($filename))."\"");
  header("Content-length: $len");
  header("Connection: close");

  if ($fp=fopen($file, "rb")) {
    fpassthru($fp);
    die;
  } else {
    echo "ERROR: $file_to_pass";
  }

Hope it helps... sorry to just give code, I know you're probably looking for why's, but I honestly don't remember the why's anymore on this project.
 
i don't think the code is wrong at all. and i think readfile is better these days than fpassthru.

i suspect that the server itself is timing out. check the connection timeout setting in the web site properties within the iis snapin.
 
I tried using skiflyer's code but it had a similar problem. Instead of getting an error it just said the download was complete even though it only downloaded 17 of 22 megabytes.

It does seem like the problem is a timeout issue. On the website tab in IIS I tried chaning the Connection timeout from 120 seconds to 6000 and it didn't help.

Any other ideas?

It's only easy when you know how.
 
remember the timeout settings are at the web site and webserver level.
also if you are running php as a cgi then you will need to check the cgi timeout (this is different from the normal timeout).

 
It was the CGI time out in IIS. By default it is set to 300 seconds. It is easy to change in IIS 5 but in IIS 6 it is quite the task. You have to download the IIS6 resource kit and edit the metabase.

These instructions were very helpfull, scroll to step 7.


Thanks for your help,
Kurt

It's only easy when you know how.
 
of course - less hassle if you use the isapi module! php recommends using this over cgi.
 
I like that my code pretends it succeeds with a partial file, what a great feature!

Glad you found the answer!
 
I like that my code pretends it succeeds with a partial file, what a great feature!
if only i had this feature for my college exams all those years ago.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top