X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Fix for selling large downloadable files. (https://forum.x-cart.com/showthread.php?t=666)

minorgod 11-15-2002 12:25 PM

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.


All times are GMT -8. The time now is 12:55 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.