Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Fix for selling large downloadable files.

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 11-15-2002, 12:25 PM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default Fix for selling large downloadable files.

In case anyone else has had problems distributing large electronic files via x-cart's Egoods module, here's my solution. Without this fix, your web server must hold the entire contents of the downloadable file in memory before/while sending it to the user's browser. For this reason, many ISPs limit the amount of memory a script can consume to a paltry 1-5MB. This is really a good thing because it keep the server from grinding to a halt when someone downloads large files (>10MB). You can replace your customer/download.php file with the following code which makes the server read the file in smaller chunks and immediately send them to the browser. It has only been moderately tested, but it's working great for me...I'm now able to sell downloadable files in excess of 35MB, whereas before, it was crapping out at 2-5MB. So here's the code:

Code:
# # $Id: download.php,v 1.6 2002/06/13 10:06:10 zorg Exp $ # # This module adds support for downloading electronicaly distributed goods # require "../smarty.php"; require "../config.php"; if (empty($id)) exit(); $query = "SELECT * FROM download_keys WHERE download_key = '$id'"; $res = func_query_first($query); # If there is corresponding key in database and not expired if ((count($res) > 0 )AND($res['expires'] > time())){ # check if there is valid distribution for this product $productid = $res['productid']; $result = func_query_first("SELECT distribution, product, provider FROM products WHERE productid = '$productid'"); $distribution = $result['distribution']; $provider = $result['provider']; if (empty($provider) || $single_mode) $distribution = $files_dir_name.$distribution; else $distribution = $files_dir_name."/$provider".$distribution; if ($fd = @fopen ($distribution, "r")){ $filesize=filesize($distribution); $fname = basename ($distribution); Header("Content-type: application/octet-stream"); Header("Content-Disposition: attachment; filename=$fname"); while(!feof($fd)) { $buffer = fread($fd, 4096); print $buffer; } fclose ($fd); exit(); } else{ # If no such distributive $smarty->assign("product", $result['product']); $smarty->display("modules/Egoods/no_distributive.tpl"); exit(); } } else { $smarty->display("modules/Egoods/wrong_key.tpl"); exit; }

Please note that I'm not using the $filesize variable for anything yet, but plan on adding that as another header item so the browser knows how big the file will be. Also, this code applies only to x-cart 3.1.3a, but if necessarry, you can most likely modify any newer version of x-cart using this code as an example.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 02:17 PM.

   

 
X-Cart forums © 2001-2020