Zubrag.com
February 07, 2012, 06:56:28 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Download Stops at 31.2 MB  (Read 2773 times)
bigtalk
Newbie
*
Posts: 2


« on: April 15, 2009, 08:02:28 pm »

I've been using the script for a while. It's pretty great!
Until recently, all the files I had set up to download were smaller files (under 10MB.) I have now put up a couple larger files (40MB-100MB.) These files stop downloading at 31.2MB.
I've been looking for a solution, but am not quick in finding one. It doesn't seem like it's a timeout issue, because it always takes a different amount of time to reach the 31.2MB cut-off. It's not file dependent because it happens to different files. It doesn't seem to be browser dependent; it happens in Firefox and in Safari.
Any ideas would be extremely appreciated!

Cheers,
BigTalk

PS. - Here's the site: diabetes.CenterForTraditionalMedicine.org/MDC. You do need to login to the site to try the download but registering is free and safe. Try downloading "Preventing & Treating Diabetes Naturally: The Native Way".

Logged
bigtalk
Newbie
*
Posts: 2


« Reply #1 on: April 16, 2009, 12:45:04 am »

I got it to work. I adapted the script from another script I found.

Here's what I did:

Just after
Code:
// Browser will try to save file with this filename, regardless original filename.
// You can override it if needed.

if (!isset($_GET['fc']) || empty($_GET['fc'])) {
  $asfname = $fname;
}
else {
  // remove some bad chars
  $asfname = str_replace(array('"',"'",'\\','/'), '', $_GET['fc']);
  if ($asfname === '') $asfname = 'NoName';
}
I replaced
Code:

// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

// download
// @readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      @fclose($file);
      die();
    }
  }
  @fclose($file);
}
with
Code:
@ob_end_clean(); //turn off output buffering to decrease cpu usage

// required for IE, otherwise Content-Disposition may be ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

// set headers
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Pragma: public");
header("Expires: 0");

// multipart-download and download resuming support
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($range, $range_end) = explode("-", $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}

$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length=$fsize;
header("Content-Length: ".$fsize);
}

/* output the file itself */
$chunksize = 1*(1024*1024); //you may want to change this
$bytes_send = 0;
if ($file = fopen($file_path, 'rb'))
{
  if(isset($_SERVER['HTTP_RANGE']))
  { fseek($file, $range); }

  while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length))
  {
    $buffer = fread($file, $chunksize);
    print($buffer); //echo($buffer); // is also possible
    flush();
    $bytes_send += strlen($buffer);
  }
  fclose($file);
}
else { die('Error - cannot open file.'); }

I know that all of those changes probably weren't totally necessary, but it did the trick.
I would love any comments.
I am also extremely interested in another post: Redirect After Download

Cheers,
BigTalk
Logged
mtg
Newbie
*
Posts: 15


« Reply #2 on: April 21, 2009, 09:00:29 am »

Looks like it's just the memory_limit in your PHP configuration. What is it currently set to? I'm betting it's only around 30MB. Reason the code you replaced it with solves the problem is because instead of loading the entire file into memory (30+ MB), you are now only loading 1MB chunks and then flushing the memory when each chunk is served. If you can't change the PHP configuration, I would leave it as is. I'd change the PHP configuration if you can though, especially if you plan on serving larger file types.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC